PHP code example of fomvasss / laravel-filterable

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

    

fomvasss / laravel-filterable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Fomvasss\Filterable\Filterable;

class Article extends Model
{
    use Filterable;
    
    protected $filterable = [
        'title' => 'like',              // http://site.test/post?filter[title]=Some+title
        'price' => 'between',           // http://site.test/post?filter[price_from]=120&filter[price_to]=380
        'category_id' => 'equal',       // http://site.test/post?filter[category_id]=2
        'status' => 'in',               // http://site.test/post?filter[status][]=publish&filter[status][]=active or http://site.test/post?filter[status]=publish|active
        'created_at' => 'between_date', //http://site.test/post?filter[created_at_from]=14.10.2018&filter[created_at_to]=24.11.2018
        'updated_at' => 'equal_date',   //http://site.test/post?filter[updated_at]=14.10.2018
        'price_exists' => 'custom',     //http://site.test/post?filter[price_exists]=1 - check exists price

        // Relation model field (for `equal` and `in`)
        'user.name' => 'like',          // http://site.test/post?filter[user.name]=Ева%20Максимовна%20Терентьева
    ];
    
    protected $searchable = [
        'name', 'email', 'contacts.city'
    ];
}

 

namespace App\Http\Controllers;

use Fomvasss\MediaLibraryExtension\MediaLibraryManager;

class HomeController extends Controller 
{
    public function index(Request $request)
    {
        $articles = \App\Model\Article::filterable($request->filter)
            ->searchable($request->q)
            ->get();

        return $articles;
    }
}
bash
php artisan vendor:publish --provider="Fomvasss\Filterable\ServiceProvider"