PHP code example of akr4m / scoping

1. Go to this page and download the library: Download akr4m/scoping library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

akr4m / scoping example snippets


use akr4m\scoping\Traits\CanBeScoped;

class Post extends Model
{
    use CanBeScoped;
}

public function __invoke(Request $request)
{
    $posts  = App\Post::withScopes($this->scopes())->get();
}

protected function scopes()
{
    return [
        // Must declare the `Scope` files
        'topic' => new TopicScope(),
        'month' => new MonthScope(),
        'year' => new YearScope(),
    ];
}

use akr4m\scoping\Scoping\Contracts\Scope;
use Illuminate\Database\Eloquent\Builder;

class TopicScope implements Scope
{
    public function apply(Builder $builder, $value)
    {
        return $builder
            ->whereHas('topics', function ($builder) use ($topic) {
                $builder->where('slug', $value);
            });
    }
}