有两个模型 BoardArticle 和 BoardArticleLocation。
$articles = BoardArticle::where('board_id', $board_id)
->orderBy("id", 'desc')
->paginate(20);
$boardlocation = BoardArticleLocation::select('location_top as top', 'location_top_text as text')
->addselect(DB::raw('CONCAT("[", group_concat(location_sub), "]") as sub'))
->where('board_article_id', $id)
->groupby('location_top')
->get();
所以,这些是我尝试过的方法。
$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');
}
此方法未查找任何项目。
我应该怎么办?