jeudi 10 mai 2018

Visit system - 'Trying to get property of non-object'

Creating a system where if i press a button i can visit a place and then un-visit if i have pressed the wrong button. this will upload to the database and delete from the database depending on whether the button has been clicked. At the moment though i am getting the error:

'Trying to get property of non-object' and the place_id seems to be the problem as its being shown as null.

Tables: places: id, etc

Visit: id, place_id, user_id, timestamps

Controller: `

      $place_id = $request['placetId']; 
      $place = Place::find($place_id); 
      $visited = Auth::user()->visitors()->where('place_id', $place_id)->first(); 

      if($visited == null) {
        $visited = new Visit(); 
        $visited->user_id = Auth::user(); 
        $visited->place_id = $place->id; 
        $visited->save(); 
return null; 
} else{`

         $visited->delete();
         return null;
         }

AJAX:

    var placeId = 0;
    var token = '';
    var urlVisit = '';
    $('.visit').on('click', function(event){
     event.preventDefault();
     placeId = event.target.parentNode.parentNode.dataset['place_id'];

$.ajax({
            method: 'POST',
            url: urlVisit,
             data: { place_id: placeId, _token: token}
     })
        .done(function() {

        });

        });

Any ideas on how to fix this and get the place_id to recognise the place that is being visited?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KbzfUF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire