Please i try to make a many to many relationship in Laravel 5.4 but i allways get this error:
BadMethodCallException in Builder.php line 2443: Call to undefined method Illuminate\Database\Query\Builder::categories()
I think my code is fine , and i can't fix it , any help please.
THank you.
THis is my code :
User model
class User extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category', 'user_categorys');
}
}
Category model
class Category extends Model
{
public function users()
{
return $this->belongsToMany('App\User', 'user_categorys');
}
}
Call method
$user = User::where('id',$id_user)->first();
foreach ($request->input("idcategs") as $value) {
$user->categories()->save($value);
}
categories()
on a query builder, not the model. Are you confident you have$users
assigned from afirst()
call in your code? – Devon