Laravel 4 拉拉维尔4:where和where在where中使用了雄辩的语言
拉威尔4号-高级哪里 我正试图检索Laravel 4 拉拉维尔4:where和where在where中使用了雄辩的语言,laravel-4,eloquent,Laravel 4,Eloquent,拉威尔4号-高级哪里 我正试图检索帖子,它们有一个$关键字,就像某个同伴,除此之外,我还想检索帖子它们有一个链接标题或内容作为$关键字。 但是,当我尝试在whereHas中使用where或whereHas时,查询没有考虑这些因素。当状态为0(不可见)或不在类别1内时,不应选择帖子项 $companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id'); 下面的代码块必须做两件事: 搜索带有
帖子
,它们有一个$关键字
,就像某个同伴
,除此之外,我还想检索帖子
它们有一个链接标题
或内容
作为$关键字
。
但是,当我尝试在whereHas
中使用where
或whereHas
时,查询没有考虑这些因素。当状态为0(不可见)或不在类别1内时,不应选择帖子项
$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');
下面的代码块必须做两件事:
标题
或内容
的帖子
项目,如$关键字
同伴的项目,如$关键字
where
和where has
中的部分
谁能帮我
将您的或where
子句包装在(..)
在whereHas
闭包中不需要where
和where
,因为它查询表
对于category\u id
,您不需要其中的,除非您想在那里传递多个id
将您的或where
子句包装在(..)
在whereHas
闭包中不需要where
和where
,因为它查询表
对于category\u id
,您不需要其中的,除非您想在那里传递多个id
将您的或where
子句包装在(..)
在whereHas
闭包中不需要where
和where
,因为它查询表
对于category\u id
,您不需要其中的,除非您想在那里传递多个id
将您的或where
子句包装在(..)
在whereHas
闭包中不需要where
和where
,因为它查询表
对于category\u id
,您不需要其中的,除非您想在那里传递多个id
感谢Jarek Tkaczyk。
他的回答几乎是正确的。我所要做的就是将where
包装在或where
中。现在我得到了Posts
,它有一个同伴
,比如$关键字
,我得到了Posts
,它在内容
或标题
中有$关键字
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
感谢Jarek Tkaczyk。
他的回答几乎是正确的。我所要做的就是将where
包装在或where
中。现在我得到了Posts
,它有一个同伴
,比如$关键字
,我得到了Posts
,它在内容
或标题
中有$关键字
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
感谢Jarek Tkaczyk。
他的回答几乎是正确的。我所要做的就是将where
包装在或where
中。现在我得到了Posts
,它有一个同伴
,比如$关键字
,我得到了Posts
,它在内容
或标题
中有$关键字
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
感谢Jarek Tkaczyk。
他的回答几乎是正确的。我所要做的就是将where
包装在或where
中。现在我得到了Posts
,它有一个同伴
,比如$关键字
,我得到了Posts
,它在内容
或标题
中有$关键字
$results = Post::whereHas('companions', function($query) use($companion_id)
{
$query->whereIn('companions.id', $companion_id)
->where('state', '=', '1');
})
->orWhere( function($q) use ( $keyword ) {
$q->where('title', 'LIKE', '%' . $keyword . '%' )
->orWhere('content', 'LIKE', '%' . $keyword . '%' );
})
->whereIn('category_id', array(1, 3))
->where('state', '=', '1')
->orderBy('menu_order', 'desc')
->get();
谢谢你的回复。当我尝试你的代码时,它什么也不返回。我基本上想要的是搜索Posts
,它说的是Companion
X,然后在Posts
内容和title
中搜索关键字X。我的代码就是这样做的,只是它不看Companion
和Posts
状态是否为1,也不看帖子
位于所需的$category\u id内。@Kordaat是的,此代码完全符合您刚才所说的。它返回帖子
,这些帖子有标题或内容,如%keyword%
和类别id=1
和状态=1
,并且与$companion\u id中的同伴id相关。只需确保$companion\u id
是一个数组,否则只需使用where
而不是where
。您的代码返回了不同的结果,因为它不关心您的大多数where
子句,这是由于随机或放置造成的。我所要做的只是将第一个where
更改为或where
。帖子
和同伴
都有一个状态
,因此两者都需要它。我已经在下面发布了正确的代码。对于具有state
的两种状态,都可以。但是拥有或where
意味着你要寻找那些贴子
与同伴
匹配的结尾,或者那些内容或标题如%word%
的贴子-这不是你想要的,因此,请修改它并检查这是否是您最终需要的。通过使用或我同时获得:贴子
,其中包含包含关键字的同伴
,我在标题
和/或内容
中获得包含关键字
的贴子
。当我像你一样使用where
时,我只会在标题和/或内容中包含关键字和关键字的同伴中发布。在最后一个案例中,我的案例中没有。我会调整我的问题