Includable
This Package provides a simple way to load a model relation by require it in the request,
You must identify the relation you wish to be included if you require it in the request, (*1)
Installation
Install the package via Composer:, (*2)
$ composer require khaled-dev/includable
Usage
first use the trait in model:, (*3)
namespace App;
use Khaled7\Includable\Includable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Includable;
/**
* The attributes that are includable,
* references to a relations method.
*
* @var array
*/
protected $includable = [
'posts',
'votes',
];
public function posts()
{
return $this->hasMany(Post::class);
}
public function votes()
{
return $this->hasMany(Vote::class);
}
// Not includable, coz not in $includable
public function subscribes()
{
return $this->hasMany(Subscribe::class);
}
}
In controller:, (*4)
// Use to load all includable of all users
User::withRequestIncludes()->get()
// Use to load the includable of this instance
$user = User::first()
$user->loadRequestIncludes();
In Request:, (*5)
localhost:8000/users?includes=posts,votes
To show the result:, (*6)
// The method `loaded` comes with this package to easily load the included relation
// Use to load the model
$this->loaded('posts');
// Use to load the model to its laravel-resource
PostResource::collection($this->loaded('posts')),
// laravel-resource
// Use to Load an opptional model to its resource
PostResource::collection($this->when($this->loaded('posts'), $this->loaded('posts'))),
// laravel-resource
// Or semply you can use `WhenLoaded` laravel builtin method
PostResource::collection($this->whenLoaded('posts'))
Better to use laravel-resource, (*7)
In result it should help you to only include the relations if you really need it, and usually use in APIs., (*8)
License
MIT © Ben Constable 2017., (*9)