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 |
Filter model by query string
Example: domain.com/route?id=1
composer require leandreaci/filterable *@dev
Create a file in your Laravel/Lumen project
<?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;
}
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Leandreaci\Filterable\Filterable;
class ExampleModel extends Model
{
use Filterable;
}
?>
<?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);
}
}
?>