【问题标题】:Eloquent where clause not taking associative array [closed]雄辩的where子句不采用关联数组[关闭]
【发布时间】:2020-08-08 08:22:36
【问题描述】:

我正在尝试使用 eloquent 将关联数组传递给 where 子句,但出现错误。

这是我正在传递的示例

$where_array = [
       'last_name' => 'Smith',
       'first_name' => 'John'
];

$match = MyClass::where($where_array)->get();

这会返回如下错误:

“消息”:“SQLSTATE [42S22]:找不到列:1054 未知列 'where 子句'中的'{\"last_name\":\"Smith\",\"first_name\":\"John\"}' (SQL: select * from my_class where {\"last_name\":\"Smith\",\"first_name\":\"John\"} 为空且 my_class.deleted_at 为空限制 1)", “异常”:“照亮\数据库\查询异常”, “文件”:“/home/vagrant/code/unity/vendor/laravel/framework/src/Illuminate/Database/Connection.php”, “行”:669,

我不知道为什么它不使用数组或者为什么它检查它是否为空。是否有一些我不包括在内的库使这种格式有效?为什么它在查询中有 Object {} 括号?它是否在某处/以某种方式变成了一个对象?

【问题讨论】:

    标签: arrays laravel eloquent where-clause associative-array


    【解决方案1】:

    根据文档,传递多个 where 参数的正确格式如下:

    您还可以将一组条件传递给 where 函数:

    $users = DB::table('users')->where([ ['状态','=','1'], ['订阅', '', '1'], ])->get();

    在您的情况下,您需要将代码更改为:

    $where_array = [
           ['last_name' ,'=', 'Smith'],
           ['first_name' ,'=', 'John'],
    ];
    
    $match = MyClass::where($where_array)->get();
    

    【讨论】:

      【解决方案2】:

      我的朋友,在 where 子句中,您需要传递至少 2 个参数,第一个是模型中您要应用条件的列的名称。

      例如: 我想在用户表中找到一个名为“hossein”的用户。

      $user = User::where('name','hossein')->first();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-15
        • 2018-09-03
        • 2015-05-17
        • 2014-09-20
        • 2022-01-07
        • 1970-01-01
        • 2014-11-05
        相关资源
        最近更新 更多