PHP code example of n7olkachev / laravel-filterable

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

    

n7olkachev / laravel-filterable example snippets


Page::filter($request->all())->get()

class Page extends Model
{
    use Filterable;

    protected $fillable = [
        'title',
        'status',
        'created_at',
    ];
    
    protected $filterable = [
        'title',
        'created_after'
    ];

    public function scopeCreatedAfter($query, $time)
    {
        return $query->where('created_at', '>', $time);
    }
}

Page::filter(['title' => 'Cool page'])->first(); // equals to where('title', 'Cool page')

Page::filter(['status' => ['new', 'active'])->get() // equals to whereIn('status', ['new', 'active'])

Page::filter(['created_after' => '2017-01-01'])->get() // equals to createdAfter('2017-01-01') (notice our scope in Page class)

Page::filter(['title' => 'Cool page', 'status' => 'active'])->first()
 

use Filterable;

protected $filterable = ['created_at', 'title'];