【问题标题】:How to pass array in laravel where condition?如何在 laravel where 条件下传递数组?
【发布时间】:2014-04-21 13:45:31
【问题描述】:
    $table='user';
    $conditions=array(
      'uname' => 'test', 
      'pwd' => '123'); //uname and pwd are the column names

    DB::table($table)->where($conditions)->get();

在执行上述代码时显示以下错误

ErrorException

strtolower() expects parameter 1 to be string, array given

谁能帮帮我。

【问题讨论】:

    标签: php mysql database frameworks laravel


    【解决方案1】:

    我不知道您使用的是哪些类,但您的 php 编译器说您传递的是数组而不是字符串,所以不妨尝试以字符串格式传递条件:

    $conditions = "uname='test'&pwd='123'";
    

    更新
    正如您在文档 (http://laravel.com/docs/queries#selects) 中看到的,您只能将字符串传递给 where 方法,因此您必须这样做:

    $select = DB::table($table);
    foreach($conditions as $column=>$value){
        $select->where($column,'=',$value);
    }
    $result = $select->get();
    

    【讨论】:

    • 我正在使用 laravel 的查询生成器。我不知道为什么它不接受 where 条件内的数组。如果我们使用这样的字符串,我们需要通过以这种方式使用它的获取结果来使用'whereraw()',但我希望通过在内部传递数组元素来输出。我希望我们有一些东西。
    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2017-02-28
    • 2018-01-31
    • 1970-01-01
    • 2016-01-19
    • 2017-10-17
    • 1970-01-01
    相关资源
    最近更新 更多