Laravel 4 拉拉维尔4:where和where在where中使用了雄辩的语言

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'); 下面的代码块必须做两件事: 搜索带有

拉威尔4号-高级哪里

我正试图检索
帖子
,它们有一个
$关键字
,就像某个
同伴
,除此之外,我还想检索
帖子
它们有一个链接
标题
内容
作为
$关键字
。 但是,当我尝试在
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
    时,我只会
    在标题和/或内容中包含关键字和关键字的
    同伴中发布
    。在最后一个案例中,我的案例中没有。我会调整我的问题