Relationship HasManyThrough with Many to Many relationship

后端 未结 1 341
无人及你
无人及你 2021-01-29 07:07

Each Article belongs to many Categories. Each User has many Categories. I want to retrieve all the Articles which has the User’s Categories. For each of these relationships bel

相关标签:
1条回答
  • 2021-01-29 07:22

    You could use an IN query. Or a subquery.

    Article::whereIn('id', $user->Categories::all->lists('id')->toArray() )
    
    
    
    $category_id = $user->Categories::all->lists('id')->toArray();
    Article::whereIn('id', function($query) use ($category_id){
       $query->select('categories_id')
         ->from(with(new Category)->getTable())
         ->whereIn('category_id', $categoryID )
    })->get();
    
    0 讨论(0)
提交回复
热议问题