简体   繁体   English

Laravel 查询生成器在 where 数组子句中返回多个列值

[英]Laravel query builder return multiple column values in where array clause

In my Laravel project a user can find data from a table via a front-end GUI that allows them to pick and choose tables and columns (with restrictions).在我的 Laravel 项目中,用户可以通过前端 GUI 从表中查找数据,允许他们选择表和列(有限制)。 I'm trying to search by multiple different values in a where clause on a column called event_action so that I can bring back multiple results, I appear to be getting some kind of results back, but I'm not seeing all of my results?我正在尝试在名为event_action的列的 where 子句中搜索多个不同的值,以便我可以带回多个结果,我似乎得到了某种结果,但我没有看到我的所有结果?

$results = DB::table('my_table')
             ->select('event_category', 'event_action', 'event_count', 'created_at')
             ->where([
               ['event_category', 'my category'],
               ['event_action', ['value1', 'value2']],
               ['created_at', '>=', '2021-03-01']
             ])
             ->get();

My event_action column has many rows where the value of some rows have value1 and others have value2 , but in my returned results I'm only seeing one or the other, not both, for instance, in Tinker:我的event_action列有很多行,其中一些行的值具有value1而其他行的值具有value2 ,但在我返回的结果中,我只看到一个或另一个,而不是两者,例如,在 Tinker 中:

=> Illuminate\Support\Collection {#3501
 all: [
   {#3433
     +"event_category": "my category",
     +"event_action": "value2",
     +"event_count": "16",
     +"created_at": "2021-03-01 01:00:03",
   },
   {#3499
     +"event_category": "my category",
     +"event_action": "value2",
     +"event_count": "16",
     +"created_at": "2021-03-01 01:15:06",
   },
   {#3464
     +"event_category": "my category",
     +"event_action": "value2",
     +"event_count": "19",
     +"created_at": "2021-03-01 01:30:05",
   },
   {#3465
     +"event_category": "my category",
     +"event_action": "value2",
     +"event_count": "21",
     +"created_at": "2021-03-01 01:45:07",
   },
   {#3466
     +"event_category": "my category",
     +"event_action": "value2",
     +"event_count": "24",
     +"created_at": "2021-03-01 02:00:05",
   },
 ],
}

For Date, use whereDate and for multiple values in single field, you can use whereIn对于日期,使用 whereDate,对于单个字段中的多个值,您可以使用 whereIn

$results = DB::table('my_table')
             ->select('event_category', 'event_action', 'event_count', 'created_at')
             ->where('event_category', '=','my category')
             ->whereDate('created_at', '>=', date('2021-03-01'))
             ->whereIn('event_action', ['value1', 'value2'])
             ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM