PHP code example of manajet / laravel-extra-resource

1. Go to this page and download the library: Download manajet/laravel-extra-resource 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/ */

    

manajet / laravel-extra-resource example snippets


$user = User::find(1);
return (new UserResource($user))->using(['foo' => 'bar']);



namespace App\Http\Resources;

use Manajet\ExtraResource\ExtraJsonResource;

class UserResource extends ExtraJsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'foo' => $this->extra['foo'],
        ];
    }
}

$users = User::all();
return (new UserCollection($users))->using(['foo' => 'bar']);

$users = User::all();
return UserResource::collection($users)->using(['foo' => 'bar']);
sh
php artisan make:extraresource MyResource
sh
php artisan make:extraresource MyCollection --collection