Answers for "laravel global scope"

PHP
0

laravel global scope

protected static function booted()
    {
        self::addGlobalScope('latest', function ($query){
            $query->latest('updated_at');
        });
    }
Posted by: Guest on August-27-2021
0

laravel global scope

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class AncientScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('created_at', '<', now()->subYears(2000));
    }
}
Posted by: Guest on May-10-2021

Browse Popular Code Answers by Language