【问题标题】:Laravel Eloquent - Multiple NOT LIKE queries?Laravel Eloquent - 多个 NOT LIKE 查询?
【发布时间】:2019-11-14 14:06:49
【问题描述】:

我有一个来自数据库的可变输出,如下所示:

1,2,3,4 有时是5,4,9

我想根据用户添加not like where 子句:

例如:

用户 1

$products = \App\Product::where('user', '1')
    ->where(['category', 'not like', '1,2,3,4')
    ->get();

用户 2

$products = \App\Product::where('user', '2')
    ->where(['category', 'not like', '5,4,9')
    ->get();

【问题讨论】:

  • 我回答了你的问题,但我觉得也许我们没有理解你的意思,如果我们的答案不是你想要的,也许让我们知道并稍微编辑一下问题。

标签: mysql laravel laravel-5 eloquent


【解决方案1】:

https://laravel.com/docs/5.8/queries#where-clauses

$products = \App\Product::where('user','2')
  ->whereNotIn('category', [5, 4, 9])
  ->get();

【讨论】:

  • 当我使用`$result = "5,4,9"; ->whereNotIn('category', [$result])` 不工作
  • 如何在你的数据库中存储产品类别?作为字符串?
  • $result 需要是这样的数组 $result = [5,4,9],而不是字符串,然后你就这样使用 ('category', $result)
【解决方案2】:

如果您只需要更改类别,为什么不将其设为这样的变量。

$categories = $userId == 1 ? [1,2,3,4] : [5,4,9];

$products = \App\Product::where('user',$userId)
  ->whereNotIn('category', $categories)
  ->get();

所以我的猜测是,你需要有一些方法来查看用户应该拥有哪些类别,这会比这更复杂,但我希望你明白这一点。

【讨论】:

    【解决方案3】:

    另一种方式

     $user_id = $request->input('id');//Put your user id in variable either by request or anyway you want to save
     $category_arr = [];
    
     if($user_id == 1) {
       $category_arr = [1,2,3,4];   
     } elseif ($user_id == 2) {
       $category_arr = [5,4,9];
     } else {
       echo "Error Msg"
     }
    
    $products = \App\Product::where('user',user_id)
       ->whereNotIn('category', $category_arr)
       ->get();
    

    【讨论】:

      【解决方案4】:

      这是一个扩展 Roman Bobrik 和 Eli's 的答案。

      目标是让您的请求能够处理默认条件,同时在需要时处理特定请求。

      if ($request->input('id') == 1) {$categories = [1,2,3,4];}
      if ($request->input('id') == 2) {$categories = [5,4,9];}
      
      $products = \App\Product::where('user', $request->input('id'))
          ->when(isset($categories), function($query) use($categories) { // This will be applied for specified users. In this case, users id = 1 || 2
              return $query->whereNotIn('category', $categories);
          })->when(!isset($categories), function($query) { // This will be applied when users id != 1 || 2
              return $query->whereNotIn('category', [1,2]); //Define your default categories for every users that are not specified at the begining.
          })->get();
      

      您还可以将类别数组保存到用户表并更改 when(Auth::user()->categories != null)when(Auth::user()->categories == null) 的 when() 条件。

      唯一的警告是,当您使用这种查询时,您必须有两个完全相反的 when() 条件。因为如果这两个条件都不符合,最终查询将是$products = \App\Product::where('user', $request->input('id'))->get();,我相信您不会发生这种情况。

      好吧,您可以链接两个以上的 when() 条件,但请确保有一个可以捕获任何其他条件无法捕获的所有内容。

      【讨论】:

        【解决方案5】:

        如果你的可变输出是一个数组,你可以试试这个:

        $categories = [1, 2, 3, 4];
        
        $products = \App\Product::where('user', 1)
           ->whereNotIn('category', $categories)
           ->get();
        

        【讨论】:

          猜你喜欢
          • 2019-02-13
          • 1970-01-01
          • 2014-10-31
          • 2020-11-21
          • 2017-11-28
          • 2017-07-30
          • 2021-05-12
          • 1970-01-01
          • 2015-07-05
          相关资源
          最近更新 更多