Ad

Call To A Member Function Adverts() On Integer

- 1 answer

I have a morphMany relationship and trying to save data to the pivot table using ajax but getting this error

"Call to a member function adverts() on integer"

My ajax code


function onBookmark()
{

$recordId = post('bookmark');

$record = Advert::find($recordId);

$user=  Auth::getUser()->id;

$user->adverts()->syncWithoutDetaching($record);

return Redirect::back();


}
Ad

Answer

You are getting the id for the user which will return an integer not the instance of the User model.

Instead try changing the following line from this

$user = Auth::getUser()->id;

to this:

$user = Auth::getUser();
Ad
source: stackoverflow.com
Ad