【发布时间】:2014-12-17 06:53:09
【问题描述】:
我想按标题和相关标签搜索帖子,这些标签与帖子具有多对多的关系。我走了很远,但有些事情让我感到困扰。这是我的代码:
$searchInput = Input::get('search');
$posts = Post::join('post_tag', 'post_tag.post_id','=','post.id')
->join('tag','tag.id','=','post_tag.tag_id')
->where('post.title', 'LIKE', '%' . $searchInput . '%')
->orWhere('tag.title', 'LIKE', '%' . $searchInput . '%')
->orderBy('post.created_at', 'desc')
->groupBy('post.id')
->with('tags')
->paginate(8);
此代码(有点)有效。假设我有一个标题为This is a test subject 并带有标签CSS 和HTML 的帖子。
- 当我搜索
test或subject时,我找到了帖子,这里没有问题。 - 当我搜索
CSS或HTML时,我找到了帖子,这里没有问题。
当我搜索上述两者的组合时(例如,当我搜索 test 和 CSS 时,我找不到任何帖子。另外,当我搜索 CSS HTML 时,我没有得到任何结果。
我希望有人能帮我优化这个搜索查询,这让我很头疼。
【问题讨论】:
标签: search laravel tags many-to-many eloquent