LaravelPackages.net
Acme Inc.
Toggle sidebar
christopherarter/laravel-resourceable

A handy method for accessing a model's value as an API resource

57
2
About christopherarter/laravel-resourceable

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

Laravel Resourceable is a package allowing you to quickly access a model's array value from an Api Resource class.

Installation

composer require christopherarter/laravel-resourceable

Examples

$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);

Why?

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());

Credit

This package was written by Chris Arter with valuable contributions by Dan Alverez

Star History Chart