I have a table of events, and a table of categories. Each event can be in more than one category, and each category can have more than event. So I have set up a pivot table category_event, containing the category_ and event_ids to match them up. I have defined models for all 3 tables:
class Event extends Model {
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
class Category extends Model {
protected $table = 'categories';
public function events()
{
return $this->belongsToMany('App\Event');
}
}
class Category_Event extends Model {
protected $table = 'category_event';
}
Then in my CategoryController I have a query:
$category = Category::with(['categories.events'])->where('id','=',$id)->get();
But this throws an error:
Call to undefined method Illuminate\Database\Query\Builder::categories()
I initially thought the error might be down to how Laravel handles naming conventions of tables and models ending with 'y', so as you can see I have explicitly specified the name of the relevant table in the models. But I still get the error.
Any ideas? TIA