vskut / laravel-api-resource-filterable
Filtering trait for Laravel 5 Api Resources.
dev-master
2019-06-16 11:47 UTC
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2025-01-07 13:03:43 UTC
README
Provides the filtering trait for Laravel 5 Api Resources.
Install
Via composer:
$ composer require vskut/laravel-api-resource-filterable
Usage
Resource
namespace App\Http\Resources; use vskut\laravel-api-resource-filterable\Filterable; use Illuminate\Http\Resources\Json\JsonResource; class UserResource extends JsonResource { use Filtratable; public function toArray($request) { return $this->filtrateFields([ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, ]); } }
Collection
namespace App\Http\Resources; use vskut\laravel-api-resource-filterable\Filterable; use Illuminate\Http\Resources\Json\ResourceCollection; class UserResourceCollection extends ResourceCollection { use Filtratable; public function toArray($request) { return [ 'data' => $this->processCollection($request), ]; } }
Controller
namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Http\Resources\UserResource; use App\User; class UserController extends Controller { public function index() { $users = User::all(); return UserCollection::make($users)->only(['id', 'name', 'email']); //return UserCollection::make($users)->except(['email']); } public function show(User $user) { return UserResource::make($user)->only(['id', 'name', 'email']); // return UserResource::make($user)->except(['email']); } }
Credits
License
The MIT License (MIT).