laravel eloquent where and code example

Example 1: laravel eloquent get first

// return first row by id
$user = App\User::where('id',$id)->first();
// or return directly a field
$userId = App\User::where(...)->pluck('id');

Example 2: eloquent where in

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

Example 3: where () laravel Eloquent

//los signos de igualdad pueden ser: ">=", "<=", "=", ">", "<"
$user = User::where("estado","=",1)->find(10);

Example 4: laravel where multiple conditions on single colmn

// here, i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();

Example 5: laravel where

$users = DB::table('users')
                ->whereMonth('created_at', '12')
                ->get();

Example 6: eloquent first

$user = User::where('mobile', Input::get('mobile'))->get();

if (!$user->isEmpty()){
    $firstUser = $user->first()
}

Tags:

Sql Example