leandreaci / filterable by leandreaci

Filter package to Laravel/Lumen
428
2
2
Package Data
Maintainer Username: leandreaci
Maintainer Contact: leandroandreaci@hotmail.com (Leandro Andreaci)
Package Create Date: 2019-11-08
Package Last Update: 2022-04-28
Language: PHP
License: Unknown
Last Refreshed: 2024-11-15 15:22:36
Package Statistics
Total Downloads: 428
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Filterable

Filter model by query string

Example: domain.com/route?id=1

Install package

composer require leandreaci/filterable *@dev

Using

Create a file in your Laravel/Lumen project

  • app/Filters/ExampleFilter.php
<?php


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;
        }
    }
    

}

  • Use the Trait Filterable to you Model want to Filter

<?php

namespace App;

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

class ExampleModel extends Model
{
    use Filterable;
}

?>

  • In Controller

<?php

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);
    }
}
?>