gpt4 book ai didi

php - 在 Laravel 5.8 中将关联数组传递给 where 子句

转载 作者:行者123 更新时间:2023-11-29 06:26:51 25 4
gpt4 key购买 nike

我有一个表,即objects并且有一些测试数据。

id    name      type      brand      model      remark1     abc       laptop    dell       2019       text2     def       laptop    dell       2019       text3     efg       laptop    dell       2018       text4     xyz       laptop    asus       2019       text

I have an array like below because I want all laptops where brand = dell AND model = 2019:

$search_arr = array(["brand" => "dell"], ["model" => "2019"]);

在我的查询中,

Objects::where(
["type", "laptop"],
$search_arr
)->get();

But, I get all rows (id => 1, 2, 3). How can I do that?

我还有另一个功能可以单独搜索品牌或型号。

为此,我准备了数组:
$search_arr = [["brand"=> "戴尔"]];
$search_arr = [["model"=> "2019"]];

它有效!!!

已编辑我一整天都在这上面度过。终于,我到达了这里。

尽我所能,我不想更改数组语法样式。如果必须的话,我只想更改查询语法。

最佳答案

您可以将您的类型添加到搜索中

$search_arr = [
"brand" => "dell",
"model" => "2019",
"type" => 'laptop'
];
Objects::where($search_arr)->get();

或者你可以说

Objects::where('brand', '=', 'dell')
->where('model', '=', '2019')
->where('type', '=', 'laptop')
->get();

就这样了😉

关于php - 在 Laravel 5.8 中将关联数组传递给 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58978256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com