2017 © Pedro Peláez
 

package includable

Help the developer to load table in relation by type the name in url

image

khaled-dev/includable

Help the developer to load table in relation by type the name in url

  • Tuesday, November 28, 2017
  • by khaled-dev
  • Repository
  • 2 Watchers
  • 1 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 6 % Grown

The README.md

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)

The Versions

28/11 2017

dev-master

9999999-dev

Help the developer to load table in relation by type the name in url

  Sources   Download

MIT

by khaled AbdElRahim

laravel api load relations with khaled-dev includable includes

28/11 2017

v1.1.0

1.1.0.0

Help the developer to load table in relation by type the name in url

  Sources   Download

MIT

by khaled AbdElRahim

laravel api load relations with khaled-dev includable includes

02/11 2017

v1.0.0

1.0.0.0

Help the developer to load table in relation by type the name in url

  Sources   Download

MIT

by khaled AbdElRahim

laravel api load relations with khaled-dev includable includes