laravel global scope

Solutions on MaxInterview for laravel global scope by the best coders in the world

showing results for - "laravel global scope"
Miranda
25 Nov 2018
1protected static function booted()
2    {
3        self::addGlobalScope('latest', function ($query){
4            $query->latest('updated_at');
5        });
6    }
Ana Sofia
08 Jan 2019
1<?php
2
3namespace App\Scopes;
4
5use Illuminate\Database\Eloquent\Builder;
6use Illuminate\Database\Eloquent\Model;
7use Illuminate\Database\Eloquent\Scope;
8
9class AncientScope implements Scope
10{
11    /**
12     * Apply the scope to a given Eloquent query builder.
13     *
14     * @param  \Illuminate\Database\Eloquent\Builder  $builder
15     * @param  \Illuminate\Database\Eloquent\Model  $model
16     * @return void
17     */
18    public function apply(Builder $builder, Model $model)
19    {
20        $builder->where('created_at', '<', now()->subYears(2000));
21    }
22}