laravel where and find code example

Example 1: find or fail laravel

$model = App\Models\Flight::findOrFail(1);

$model = App\Models\Flight::where('legs', '>', 100)->firstOrFail();

Example 2: laravel find by

// Retrieve a model by its primary key...
$flight = App\Flight::find(1);

// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();

// Shorthand for retrieving the first model matching the query constraints...
$flight = App\Flight::firstWhere('active', 1);

Example 3: laravel where

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

Example 4: laravel where

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

Example 5: laravel update first

Model::where(['x' => 1, 'y' => 2])->first()->update([...]);

Tags:

Sql Example