PHP code example of leandreaci / filterable

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

    

leandreaci / filterable example snippets





namespace App;


use Carbon\Carbon;
use Leandreaci\Filterable\QueryFilter;

class ExampleFilter extends QueryFilter
{

    public function id($id)
    {
        return $this->builder->where('id', $id);
    }

    public function start($date)
    {
        try{
            $formattedDate = Carbon::createFromFormat('Y-m-d', $date)->startOfDay()->toDateTimeString();
            return $this->builder->where('created_at','>', $formattedDate);
        }catch (\Exception $exception)
        {
            return $this->builder;
        }
    }
    

}





namespace App;

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

class ExampleModel extends Model
{
    use Filterable;
}





namespace App\Http\Controllers;

use App\ExampleModel;

class TransactionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param ExampleFilter $filter
     * @return TransactionsCollection
     */
    public function index(ExampleFilter $filter)
    {
        return  ExampleModel::filter($filter)
                               ->paginate(10);
    }
}