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
1# If you would like to generate a database migration when you
2# generate the model, you may use the --migration or -m option:
3
4php artisan make:model Flight --migration
5php artisan make:model Flight -m
1namespace App;
2
3use Illuminate\Database\Eloquent\Model;
4
5class Post extends Model
6{
7 protected $table = 'posts';
8
9 protected $fillable = ['title', 'slug', 'content'];
10
11 protected static function boot()
12 {
13 parent::boot();
14 static::saving(function ($model) {
15 $model->slug = str_slug($model->title);
16 });
17 }
18}
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Builder;
6use Illuminate\Database\Eloquent\Model;
7
8class User extends Model
9{
10 /**
11 * The "booted" method of the model.
12 *
13 * @return void
14 */
15 protected static function booted()
16 {
17 static::addGlobalScope('age', function (Builder $builder) {
18 $builder->where('age', '>', 200);
19 });
20 }
21}