PHP code example of petrelli / scoped-controller

1. Go to this page and download the library: Download petrelli/scoped-controller 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/ */

    

petrelli / scoped-controller example snippets


protected $entity = Book::class;


use Petrelli\ScopedController\BaseController;

class EventsController extends BaseController {

// Here as an example we have two scopes: year and author.
// They will be called if we receive the parameters
// byYear and byAuthor respectively.
protected $scopes = [
    'byYear'   => 'year',
    'byAuthor' => 'author',
];

}

$items = $this->collection()->get();
$items = $this->collection()->paginate(static::PER_PAGE);


protected function beginOfAssociationChain()
{
    return Book::published()->where('library', 'NYC');
}

$items = $this->collection()->get();
$items = $this->collection()->paginate(static::PER_PAGE);



// Controller function
public function index()
{
    $items = $this->applyScopes(Book::query())->get();
}


// Here as an example we have two scopes: year and author.
// They will be called if we receive the parameters
// byYear and byAuthor respectively.
protected $scopes = [
    'byYear'   => 'year',
    'byAuthor' => 'author',
    'sortBy'   => ['sort_by' ['field', 'direction']],
];


// Returns true/false
$this->hasAnyScope()



public function index()
{
    // Return Books triggering defined scopes
    $items = $this->collection()->get();
}

public function indexBestSellers()
{
    // Return Books triggering defined scopes + bestSeller scope
    $items = $this->collection()->bestSeller()->get();
}

public function scopeSortBy($query, $value, $direction = 'asc')
{
    //...
}