A handy method for accessing a model's value as an API resource
christopherarter/laravel-resourceable is a Laravel package for a handy method for accessing a model's value as an api resource.
It currently has 2 GitHub stars and 57 downloads on Packagist.
Install it with composer require christopherarter/laravel-resourceable.
Discover more Laravel packages by christopherarter
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Resourceable is a package allowing you to quickly access a model's array value from an Api Resource class.
composer require christopherarter/laravel-resourceable
$user = new User([...]);
return $user->toResourceArray();
This package will automatically look for the resource class defined in the App\Http\Resources namespace with your ClassName + Resource appended. So for our user example, it would resolve to App\Http\Resources\UserResource.
You may also use a custom API Resource class from any namespace as a parameter:
return $user->toResourceArray(AlternateResource::class);
This allows for very accurate API HTTP testing of an endpoint, for example:
// UserTest.php
$user = factory(\App\User::class)->create();
$this->get(route('users.show'))
->assertJson($user->toResourceArray());
This package was written by Chris Arter with valuable contributions by Dan Alverez