【发布时间】:2015-02-10 18:37:55
【问题描述】:
Laravel 4 - 高级哪里
我正在尝试检索具有$keyword 之类的Companion 的Posts,除此之外,我还想检索Posts 具有链接title 或content 作为@987654327 @。
但是当我尝试在whereHas 中使用where 或whereIn 时,查询不会考虑这些。当状态为 0(不可见)或不在类别 1 内时,不应选择 Post 项。
$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');
下面的代码块要做两件事:
- 搜索带有
title或content的Post项目,例如$keyword -
和搜索具有
Companions的Post项目,例如$keyword
代码:
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', 1)
->whereIn('category_id', array(1));
})
->whereIn('category_id', array(1))
->orwhere('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' )
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
除了whereHas 中的where 和whereIn 部分之外,上面的代码成功检索数据。
谁能帮帮我?
【问题讨论】: