I have three models: User, Category, Announcement
.
User model:
public function categories()
{
return $this->belongsToMany("App\Category");
}
public function announcements()
{
return $this->hasManyThrough('App\Announcement', 'App\Category');
}
Category model:
public function announcements()
{
return $this->belongsToMany('App\Announcement');
}
Announcement model:
public function categories()
{
return $this->belongsToMany('App\Category');
}
I need to select all announcements through user_categories
table.
For this I do:
$res = User::where("id", 1)->with("announcements")->get();
dd($res);
In result I get SQL error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'announcements.category_id' in 'on clause' (SQL: select `announcements`.*, `categories`.`user_id` from `announcements` inner join `categories` on `categories`.`id` = `announcements`.`category_id` where `categories`.`user_id` in (1) and `announcements`.`deleted_at` is null)
Why Laravel does try to search ``announcements.
category_id` in table?
In conclusion I need to get all announcements on categories which user subscribed.
via Darama