将关联数组传递到Laravel 5.8中的caluse位置

时间:2019-11-21 15:22:35

标签: php mysql arrays laravel eloquent

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

id    name      type      brand      model      remark

1     abc       laptop    dell       2019       text
2     def       laptop    dell       2019       text
3     efg       laptop    dell       2018       text
4     xyz       laptop    asus       2019       text

我有一个类似下面的数组,因为我想要所有的笔记本电脑where brand = dell AND model = 2019

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

在我的查询中,

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

但是,我得到了所有行(id => 1、2、3)。我该怎么办?

我还有另一个功能,可以分别搜索品牌或型号。

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

有效!

已编辑 我整天都在花时间。最后,我到达了这里。

我尽可能不希望更改数组语法样式。如果需要,我只想更改查询语法。

1 个答案:

答案 0 :(得分:2)

您可以将type添加到搜索中

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

或者您可以说

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

就这样?