PHP code example of emeguan / laravel-resources

1. Go to this page and download the library: Download emeguan/laravel-resources library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emeguan / laravel-resources example snippets


class A extends Model
{
    use HasFactory;
    use \EmeGuan\Resources\ModelTrait;  //<--

Route::get('/as', function ()
{
    $as=A::all();
    return new \EmeGuan\Resources\Collection($as);
});

Route::get('/a/{id}', function ($id)
{
    $a=A::find($id);
    return new \EmeGuan\Resources\Resource($a);  //<--
});

class A extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name'];

    public function bs()
    {
        return $this->hasMany(B::class);
    }
    
    public function ds()
    {
        return $this->hasMany(D::class);
    }
}

class B extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'a_id'];

    public function cs()
    {
        return $this->hasMany(C::class);
    }

    public function a()
    {
        return $this->belongsTo(A::class);
    }
}

class C extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'b_id'];

    public function b()
    {
        return $this->belongsTo(B::class);
    }
}

class D extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'a_id'];

    public function es()
    {
        return $this->hasMany(E::class);
    }

    public function a()
    {
        return $this->belongsTo(A::class);
    }
}

class E extends Model
{
    use HasFactory;
    use ModelTrait;

    protected $fillable = [ 'id', 'name', 'd_id'];

    public function d()
    {
        return $this->belongsTo(D::class);
    }
}