php - 如何在 laravel where 条件中传递数组?

标签 php mysql database frameworks laravel

    $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 编译器说你传递了数组而不是字符串,所以也许尝试以字符串格式传递条件:

$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();

关于php - 如何在 laravel where 条件中传递数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22435985/

相关文章:

php - {$variable} 在 PHP 中如何工作?

php - 在 PHP 函数中打开数据库连接

php - MySql 多表还是一张大表?

php - 将时间从mysql插入日期表行

database - PostgreSQL中表锁和行锁是如何交互的?

.net - 同步框架入门

用于从 google 分析帐户检索数据的 PHP 代码

mysql - mysql存储过程中的多选择语句

mysql - SQL - 从两个表中识别匹配值/记录

mysql - 如何找到从事完全相同项目的 worker 数量