laravel model events

Solutions on MaxInterview for laravel model events by the best coders in the world

showing results for - "laravel model events"
Manuel
13 Jan 2020
1# The easiest way to create a model instance is using the 
2# make:model Artisan command:
3
4php artisan make:model Flight
5
6# If you would like to generate a database migration when you 
7# generate the model, you may use the --migration or -m option:
8
9php artisan make:model Flight --migration
10php artisan make:model Flight -m
Mía
01 May 2018
1    /**
2     * The attributes that are mass assignable.
3     */
4    protected $fillable = [
5      					   'title',
6                           'slug',
7                           'body',
8                           'image',
9                           'published',
10                           'comments_open'
11                          ];
Juan José
21 May 2020
1// Retrieve flight by name, or create it if it doesn't exist...
2$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
3
4// Retrieve flight by name, or create it with the name, delayed, and arrival_time attributes...
5$flight = App\Flight::firstOrCreate(
6    ['name' => 'Flight 10'],
7    ['delayed' => 1, 'arrival_time' => '11:30']
8);
9
10// Retrieve by name, or instantiate...
11$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);
12
13// Retrieve by name, or instantiate with the name, delayed, and arrival_time attributes...
14$flight = App\Flight::firstOrNew(
15    ['name' => 'Flight 10'],
16    ['delayed' => 1, 'arrival_time' => '11:30']
17);
Lautaro
13 Jan 2019
1DB::table('users')
2            ->where('id', 1)
3            ->update(['votes' => 1]);
Jesús
25 Apr 2020
1// If there's a flight from Oakland to San Diego, set the price to $99.
2// If no matching model exists, create one.
3$flight = App\Models\Flight::updateOrCreate(
4    ['departure' => 'Oakland', 'destination' => 'San Diego'],
5    ['price' => 99, 'discounted' => 1]
6);
Simón
10 Nov 2020
1class User extends Model 
2{
3
4    public static function boot()
5    {
6        parent::boot();
7
8        self::creating(function($model){
9            // ... code here
10        });
11
12        self::created(function($model){
13            // ... code here
14        });
15
16        self::updating(function($model){
17            // ... code here
18        });
19
20        self::updated(function($model){
21            // ... code here
22        });
23
24        self::deleting(function($model){
25            // ... code here
26        });
27
28        self::deleted(function($model){
29            // ... code here
30        });
31    }
32
33}
34
similar questions
queries leading to this page
laravel model events