【问题标题】:Laravel 4: A where and whereIn inside a whereHas using EloquentLaravel 4:使用 Eloquent 在 whereHas 中的 where 和 whereIn
【发布时间】:2015-02-10 18:37:55
【问题描述】:

Laravel 4 - 高级哪里

我正在尝试检索具有$keyword 之类的CompanionPosts,除此之外,我还想检索Posts 具有链接titlecontent 作为@987654327 @。 但是当我尝试在whereHas 中使用wherewhereIn 时,查询不会考虑这些。当状态为 0(不可见)或不在类别 1 内时,不应选择 Post 项。

$companion_id = Companion::where('name', 'LIKE', '%' . $keyword . '%' )->lists('id');

下面的代码块要做两件事:

  1. 搜索带有titlecontentPost 项目,例如$keyword
  2. 搜索具有CompanionsPost 项目,例如$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 中的wherewhereIn 部分之外,上面的代码成功检索数据。

谁能帮帮我?

【问题讨论】:

    标签: laravel-4 eloquent


    【解决方案1】:
    1. orWhere 子句包装在(..)
    2. whereHas 闭包中不需要wherewhereIn,因为它查询companions
    3. category_id 不需要 whereIn,除非你想在那里传递多个 id

    .

    $results = Post::whereHas('companions', function($query) use($companion_id)
        {
            $query->whereIn('companions.id', $companion_id);
        })
        ->whereIn('category_id', array(1)) // why not where(..) ?
        ->where(function ($q) use ($keyword) {
          $q->where('title', 'LIKE', '%' . $keyword . '%' )
            ->orWhere('content', 'LIKE', '%' . $keyword . '%' );
        })
        ->where('state', '=', '1')
        ->orderBy('menu_order', 'desc')
        ->get();
    

    【讨论】:

    • 感谢您的回复。当我尝试您的代码时,它什么也不返回。我基本上想要的是搜索Posts,上面写着Companion X,并在Posts contenttitle 中搜索关键字X。我的代码可以做到这一点,除非它不看Companion Posts 状态为 1,并且它不会查看 Posts 是否在所需的 $category_id 内。
    • @Kordaat 是的,这段代码完全符合您刚才所说的。它返回具有title or content like %keyword%category_id = 1state = 1 并与companion id in $companion_id 相关的posts。只要确保$companion_id 是一个数组,否则只需使用where 而不是whereIn。您的代码返回了不同的结果,因为它不关心您的大部分 where 子句,因为 random or 放置。
    • 我只需要更改第一个 whereorWherePostscompanions 都有一个 state,所以两者都需要它。我在下面发布了正确的代码。
    • 两个都可以state。但是有orWhere 意味着你要寻找那些postscompanions 匹配闭包 那些content or title like %word% - 这不是你要求的,所以修改它并检查这是否是你最终需要的。
    • 通过使用 or 我得到了两个:Posts 有一个包含关键字的 Companion,我得到了 Posts 包含 @987654362 中的 keyword @ 和/或 content。当我像您发布的那样使用where 时,我只会在Companion 包含标题和/或内容中的关键字 关键字时使用Posts。在最后一种情况下,我的情况没有。我会调整我的问题。
    【解决方案2】:

    感谢 Jarek Tkaczyk。 他的回答几乎是正确的。我所要做的就是将where 包裹在orWhere 中。现在我得到了Posts,它有一个Companion,比如$keyword,我得到了Posts,它在contenttitle 内有$keyword

    $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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2021-07-01
      • 2019-01-22
      • 2021-12-03
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多