Laravel: whereIn 不使用给定的数组作为参数

Laravel: whereIn does not use given array as argument

这里有两行代码,看起来很像。

   1: $q = \App\User::whereIn('id',[1,2,3]);

   2: $users = DB::table('users')->whereIn('id', array(1, 2, 3));

但是结果不一样

第 1 行没有 select 任何结果。它的集合是空的。 第 2 行可以 select 纠正其用户 ID 在给定范围内的用户。

谁能给出原因?

这里是命令行的更多输出。

>>> $users = DB::table('users')->whereIn('id', array(1, 2, 3));
=> <Illuminate\Database\Query\Builder #00000000201a36590000000030994e20> {
       aggregate: null,
       columns: null,
       distinct: false,
       from: "users",
       joins: null,
       wheres: [
           [
               "type"    => "In",
               "column"  => "id",
               "values"  => [
                   1,
                   2,
                   3
               ],
               "boolean" => "and"
           ]
       ],
       groups: null,
       havings: null,
       orders: null,
       limit: null,
       offset: null,
       unions: null,
       unionLimit: null,
       unionOffset: null,
       unionOrders: null,
       lock: null
   }

>>> $q = \App\User::whereIn('id',[1,2,3]);
=> <Illuminate\Database\Eloquent\Builder #0000000018080e2500000000179a03c5> {}

这里是 User.php

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

        use Authenticatable, CanResetPassword;

        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['name', 'email', 'password'];

        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = ['password', 'remember_token'];

}

这里是 "get()" 输出。

>>> \App\User::whereIn('id',[1,2,3])->get();
=> <Illuminate\Database\Eloquent\Collection #000000007833eb04000000000d6d8434> {}
>>> DB::table('users')->whereIn('id', array(1, 2, 3))->get();
=> [
       <stdClass #000000007833eb1e000000000d6d8434> {
           id: 1,
           name: "test",
           email: ****,
           password: ****,
           remember_token: null,
           created_at: "2015-03-23 03:32:32",
           updated_at: "2015-03-30 03:13:34",        
       }

两者应该工作相同,但您应该 运行 使用 get:

的查询
$q = \App\User::whereIn('id',[1,2,3])->get();

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();