scope function in laravel code example
Example 1: how to create model in laravel
php artisan make:model Flight
Example 2: model observer laravel
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = 'posts';
protected $fillable = ['title', 'slug', 'content'];
protected static function boot()
{
parent::boot();
static::saving(function ($model) {
$model->slug = str_slug($model->title);
});
}
}
Example 3: laravel scope
public function apply(Builder $builder, Model $model)
{
$builder->where('age', '>', 200);
}
Example 4: laravel scope query
$model = App\Models\Flight::where('legs', '>', 100)
->firstOr(['id', 'legs'], function () {
// ...
});