Mohammad Mansour
Mohammad Mansour

Reputation: 25

Error: Call to a member function stores() on null

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

Upvotes: 0

Views: 45

Answers (1)

Ali Raza
Ali Raza

Reputation: 932

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()...
}

Upvotes: 2

Related Questions