0

Here are two lines of code which looks quite the same.

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

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

But its result are different.

The line 1 does not select any result. Its collection is empty. The line 2 could select correct users where its user id is in the given range.

Could someone give a reason why?

Here is more output from command line.

>>> $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> {}

And here is the 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'];

}

And here is the "get()" output.

>>> \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",        
       }

1 Answer 1

1

Both should work the same, but you should run the query using get:

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

and

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.