Skip to content

Filterable methods (only/except) for laravel Api Resources (eloquent-resources)

Notifications You must be signed in to change notification settings

VSKut/laravel-api-resource-filterable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Laravel Api Resource filterable

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).

About

Filterable methods (only/except) for laravel Api Resources (eloquent-resources)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages