Laravel-如何合并查询
有两种模型BoardArticle和BoardArticleLocationLaravel-如何合并查询,laravel,eloquent,laravel-models,Laravel,Eloquent,Laravel Models,有两种模型BoardArticle和BoardArticleLocation $articles = BoardArticle::where('board_id', $board_id) ->orderBy("id", 'desc') ->paginate(20); 这就是我尝试的方法 $articles = BoardArticle::where('board_id', $searchKey, $board_id) ->selec
$articles = BoardArticle::where('board_id', $board_id)
->orderBy("id", 'desc')
->paginate(20);
这就是我尝试的方法
$articles = BoardArticle::where('board_id', $searchKey, $board_id)
->select('board_articles.*')
->addselect(DB::raw('CONCAT("[", group_concat(DISTINCT location_top_text), "]") as location_text'))
->leftjoin('board_article_locations', 'board_articles.id', '=', 'board_article_locations.board_article_id')
->groupby('board_articles.id')
->orderBy("id", 'desc')
->paginate(20);
这个方法太慢了
$articles = BoardArticle::where('board_id', $searchKey, $board_id)
->with('BoardArticleLocations')
->orderBy("id", 'desc')
->paginate(20);
Models\BoardArticleLocation
public function BoardArticleLocations()
{
return $this->hasMany(BoardArticleLocation::class)
->select('location_top as top', 'location_top_text as text')
->addselect(DB::raw('CONCAT("[", group_concat(location_sub), "]") as sub'))
->groupby('location_top');
}
此方法未查找任何项
我该怎么办
$articles = BoardArticle::where('board_id', $searchKey, $board_id)
->with('BoardArticleLocations')
->orderBy("id", 'desc')
->paginate(20);
Models\BoardArticleLocation
public function BoardArticleLocations()
{
return $this->hasMany(BoardArticleLocation::class)
->select('location_top as top', 'location_top_text as text')
->addselect(DB::raw('CONCAT("[", group_concat(location_sub), "]") as sub'))
->groupby('location_top');
}