PHP code example of mobileka / scope-applicator-laravel

1. Go to this page and download the library: Download mobileka/scope-applicator-laravel 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/ */

    

mobileka / scope-applicator-laravel example snippets


 namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use App\Models\Post;

class PostController extends Controller
{
    public function index()
    {
        return Post::all();
    }
}

 namespace App\Models;

use Mobileka\ScopeApplicator\Laravel\Model;

class Post extends Model
{
    public function scopeUserId($builder, $param = 0)
    {
        if (!$param) {
            return $builder;
        }
        
        return $builder->where('user_id', '=', $param);
    }
}

 namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use App\Models\Post;

class PostController extends Controller
{
    // an array of available scopes
    public $scopes = [
        'userId'
    ];

    public function index()
    {
        return Post::handleScopes($this->scopes)->get();
    }
}

 namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use App\Models\Post;

class PostController extends Controller
{
    // an array of available scopes
    public $scopes = [
        'userId' => [
            // Here it is!
            'alias' => 'author_id'
        ]
    ];

    public function index()
    {
        return Post::handleScopes($this->scopes)->get();
    }
}

 namespace Acme\Repositories;

class BaseRepository
{
    protected $dataProvider;
    
    public function __construct($dataProvider)
    {
        $this->dataProvider = $dataProvider;
    }
    
    public function getDataProvider()
    {
        return $this->dataProvider;
    }
    
    public function all()
    {
        return $this->getDataProvider()->all();
    }
}

 namespace Acme\Repositories;

use Mobileka\ScopeApplicator\Laravel\Repository;

class BaseRepository extends Repository
{
    protected $dataProvider;
    
    public function __construct($dataProvider)
    {
        $this->dataProvider = $dataProvider;
    }
    
    public function getDataProvider()
    {
        return $this->dataProvider;
    }
    
    public function all($scopes = [])
    {
        // This part has to be noticed!
        return $this->applyScopes($this->getDataProvider(), $scopes)->get();
    }
}