Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WhereIn with Eloquent in laravel?

I want to used WhereIn method in Eloquent but it now work as below function

Messages: Call to undefined method Illuminate\Database\Query\JoinClause::whereIn()

 Class Notificatin extends Model{

 public function getNotification($user_id)
    {

        $this->_data = self::select('*')
            ->join('user_permission', function($join){
                $join->on('n_user_id','=','user_id')->whereIn('permission_id',array(90,91,92,93));
            })
            ->get();
        if (count($this->_data)) {
            return $this->_data;
        } else {
            return $this->_data;
        }
    }
   }
like image 651
DMS-KH Avatar asked Apr 05 '16 04:04

DMS-KH


2 Answers

You need to modify you join query and put whereIn clouse out side like:

$this->_data = self::select('*')
            ->join('user_permission', function($join){
                $join->on('n_user_id','=','user_id');
            })->whereIn('user_permission.permission_id',array(90,91,92,93))
            ->get();
like image 68
Ali Avatar answered Sep 19 '22 00:09

Ali


Without using Joins :

whereIn / whereNotIn / orWhereIn / orWhereNotIn

The whereIn method verifies that a given column's value is contained within the given array:

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

The whereNotIn method verifies that the given column's value is not contained in the given array:

$users = DB::table('users')
                ->whereNotIn('id', [1, 2, 3])
                ->get();
like image 33
Aslam Khan Avatar answered Sep 19 '22 00:09

Aslam Khan