【问题标题】:How to write an Eloquent query in Laravel 8 for an unknown number of orWhere conditions如何在 Laravel 8 中为未知数量的 orWhere 条件编写 Eloquent 查询
【发布时间】:2021-12-03 07:36:37
【问题描述】:

我一直在寻找和寻找,我找不到这个问题的答案。我需要查询我将有动态数量的OR 子句。 documentation 似乎表明可以将一系列条件传递给 whereorWhere,但是当我尝试这样做时,我最终没有结果。

我需要做的查询看起来像这样:

SELECT * FROM tags WHERE name = $name1 OR name = $name2 ... OR name = $nameN;

我创建了一个条件数组并将它们传递给orWhere,如下所示:

// a user's tags for which all corresponding questions are queried
$user = User::with('tags')-where('id', Auth::user()->id)-get();

// an array of conditions formatted like the docs specify
$conditions = $user->tags->map(function ($item) {
    return ['name', '=', $item->name];
})->all();
/* 
outputs a plain array like this: 

[
  ["name", "=", "php"],
  ["name", "=", "mysql"],
  ["name", "=", "laravel"],
]
*/

$tags = Tag::with([
    'questions.answers', 
    'questions.user.answers',
    'questions.tags'
])
->orWhere($conditions)
->get();

结果应该是与用户标签匹配的标签集合,以及他们的相关问题。但相反,我什么也没得到。我已经尝试以多种不同的方式重新编写此查询,并且也没有急切地加载相关数据,但没有任何区别。如果我在orWhere 子句之前添加where 子句,则查询仅返回由where 子句过滤的数据。

我对此束手无策。我想出了一个解决方法,但它很复杂而且真的不理想。我应该能够只使用带有一系列条件的简单orWhere,而不是诉诸复杂的黑客攻击。谁能指出我做错了什么和/或我如何才能达到我想要得到的结果?提前致谢。

【问题讨论】:

    标签: php mysql laravel eloquent laravel-8


    【解决方案1】:

    你有没有尝试过这样的事情:

    // For the first time, you need to use where statement
    $tags = Tag::where(...);
    
    // Then for the remaining conditions, you could use looping
    // for chaining the orWhere statements
    foreach ($queryStatements as $statement) {
        $tags = $tags->orWhere(...);
    }
    
    // Then after all queries have been applied, call the get() method
    $res = $tags->get();
    

    我就是这样做的,希望对你有帮助!

    【讨论】:

    • 我确实尝试了这个版本,但只应用了原始的 where 子句。就好像我根本没有指定任何orWhere 条件一样。但是,我会试试你的例子。非常感谢您的意见。
    • 您能否指定您尝试该方法的代码?恐怕您的方法不起作用,因为您在 where() 语句之后立即调用了 get() 方法,因此未应用 orWhere() 语句。 @迈克尔库克
    【解决方案2】:

    如果你在orWhere() 方法中放置一个数组。 您的查询将是这样的:

    `select * from `tags` where (`name` = ? and `name` = ? and `name` = ?) and `tags`.`deleted_at` is null`
    

    正如您所看到的,它正在搜索 and 以查找您放入的每个项目。 所以这是我解决问题的方法:

    $query = Tag::with([
        'questions.answers',
        'questions.user.answers',
        'questions.tags'
    ])->query();
    //Foreach item in array
    foreach ($datas as $data) {
        $query->orWhere('name', '=', $data);
    }
    //Excute the query
    $tags = $query->get();
    

    希望这能解决您的问题。

    【讨论】:

      【解决方案3】:

      如果您只想按名称查找标签,可以使用whereIn() 方法。

      // Get array of current authenticated user's tags
      $tags = auth()->user()->tags()->pluck('name');
      
      /*
          outputs an array like this:
          ['php', 'laravel', 'mysql']
      */
      
      $tags = Tag::with([
          'questions.answers',
          'questions.user.answers',
          'questions.tags'
      ])
          ->whereIn('name', $tags)
          ->get();
      


      注意:whereIn() 方法的第一个参数是您要搜索的列名,第二个参数是您要搜索的值数组。

      【讨论】:

        猜你喜欢
        • 2015-02-22
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        • 2020-01-01
        • 2021-11-06
        • 1970-01-01
        • 2019-09-21
        • 2022-01-03
        相关资源
        最近更新 更多