dev-master
9999999-devLaravel package API Result filtering, sorting & searching.
The Requires
- php >=7.0.0
- sofa/eloquence 5.5.*
by Manny Isles
laravel api filter search sorting
Laravel package API Result filtering, sorting & searching.
Laravel package API Result filtering, sorting & searching., (*1)
Require this package with composer., (*2)
composer require mannysoft/hanap
Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider., (*3)
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mannysoft\Hanap\FilterTrait; use Mannysoft\Hanap\FilterScope; class Team extends Model { use FilterTrait; protected $fillable = ['name']; protected $filters = ['status']; // field to filter ?status=active protected $sorts = ['name', 'status']; // fields to ?sort=name ?sort=-name ?sort=name,-status protected $searchableColumns = ['name']; // fields to search /** * The "booting" method of the model. * * @return void */ protected static function boot() { parent::boot(); static::addGlobalScope(new FilterScope); } }
Now you can use in your model., (*4)
// Will automatically run the filters, sorts and search $teams = Team::all();
GET /teams?status=active // Get active teams GET /teams?sort=-name,created_at // Retrieves a list of teams in descending order of name. Within a specific name, older teams are ordered first GET /teams?q=manny // Retrieves data mentioning the word 'manny' GET /teams?fields=id,name // Retrieves fields 'id' and 'name'```php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\Resource; class TeamResource extends Resource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return show_fields([ 'id' => $this->id, 'name' => $this->name, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]); } }```json { "data": [ { "id": 1, "name": "Dream Team" }, { "id": 2, "name": "Team 1" } ] }
Laravel package API Result filtering, sorting & searching.
laravel api filter search sorting