laravel 8 create model code example
Example 1: laravel create model
php artisan make:model Flight
php artisan make:model Flight --migration
php artisan make:model Flight -m
Example 2: php artisan make model
php artisan make:model Flight
Example 3: laravel update
<!-mass update-->
App\Models\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);
Example 4: laravel update
$flight = App\Models\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
Example 5: eloquent firstOrCreate
firstOrCreate() will automatically create a new entry in the database if there is not match found. Otherwise it will give you the matched item.
firstOrNew() will give you a new model instance to work with if not match was found, but will only be saved to the database when you explicitly do so (calling save() on the model). Otherwise it will give you the matched item.
Example 6: laravel scope query
$model = App\Models\Flight::where('legs', '>', 100)
->firstOr(['id', 'legs'], function () {
});