Home > Back-end >  Error: Call to a member function stores() on null
Error: Call to a member function stores() on null

Time:10-28

I have 2 Tables Users and Stores the relation between them is a Pivot table called user_store

User::find($user_id)->stores()
                ->sync(Store::whereIn('id', $store_ids)->get());

i know it's not very clear but if you have idea what is the cause of the problem

CodePudding user response:

It is saying that you are calling stores on user which happens to be null:

Depending on PHP version:

User::find($user_id)?->stores() ... // null safety

OR

$user = User::find($user_id);
if($user != null){
  $user->stores()...
}
  • Related