mhamzeh/presenter-filter is a Laravel package.
It currently has 11 GitHub stars and 20 downloads on Packagist (latest version 0.1).
Install it with composer require mhamzeh/presenter-filter.
Discover more Laravel packages by mhamzeh
or browse all Laravel packages to compare alternatives.
Last updated
The package allows you to create two types of classes: filter class and presenter class
This package allows you to easily handle database filtering through query strings.
example: /users?status=1&name='kami'
Sometimes our models get too fat, and that can make our development a lot harder. In this case, we use a second class that has the same function as the model and is used as a second model, and the Harrow method can be included in this class.
you can install the package via composer:
composer require mhamzeh/presenter-filter
You Must by publishing configuration by issuing following artisan command php artisan vendor:publish.
You have access to two commands and you can use them to create your own filter and presenter classes
You can use the following command to create a new filter.
php artisan make:filter UserFilter
This will create a new filter in the app/Filters directory.
options:
1-You can add the model to the command
php artisan make:filter UserFilter --model=User
Used by default Models folder If you have saved models elsewhere, change the config Modules of this folder
You can use the following command to create a new Presenter
php artisan make:presenter UserPresenter
options:
1- You can add the model to command
php artisan make:presenter UserPresenter --model=User
Let's say you want to use filterable on User model. You will have to create the filter class App/Filters/PostFilter.php (php artisan make:filter PostFilter --model=Post)
If you use the --model option, filterable will be added directly to the model
<?php
namespace App\Filters;
use mhamzeh\packageFp\Filters\contracts\QueryFilter;
class UserFilter extends QueryFilter
{
public function name($value){
return $this->builder->where('name','LIKE',"%$value%");
}
}
Now you need to add local scope to your model if you have not used the --model option:
use mhamzeh\packageFp\Filters\contracts\Filterable;
...
class User extends Model
{
use Filterable;
...
}
Finaly, call the scope in controller like so:
use App\Filters\UserFilter;
...
public function index()
{
return User::filters(new UserFilter())->paginate();
}
Let's say you want to use Presentable And introduce the presenter class on User model. You will have to create the filter class App/Presenter/UserPresenter.php (php artisan make:presenter UserPresenter --model=User)
If you use the --model option, Presentable and presenter class will be added directly to the model
<?php
namespace App\Presenter;
use mhamzeh\packageFp\Presenter\Contracts\Presenter;
class UserPresenter extends Presenter
{
public function full_name(){
return $this->entity->name ." ".$this->entity->full_name;
}
}
Now you need to add local scope to your model if you have not used the --model option:
use mhamzeh\packageFp\Presenter\Contracts\Presentable;
...
class User extends Model
{
use Presentable;
protected $presenter = UserPresenter::class;
...
}
Finally you can use this method in your Blade or Api Resources or Controller for example Blade:
@foreach($users as $user)
<div>
<p>fullname : $user->present()->full_name </p>
</div>
@endforeach
in Api Resources:
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id'=>$this->id,
'full_name'=> $this->present()->full_name,
'email'=>$this->email
];
}
}