PHP code example of netsells / laravel-resourceful
1. Go to this page and download the library: Download netsells/laravel-resourceful 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/ */
netsells / laravel-resourceful example snippets
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'avatar_url' => $this->avatar->url,
];
}
}
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends \Illuminate\Routing\Controller
{
public function index()
{
return UserResource::collection(User::paginate());
}
}
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
public function index()
{
return UserResource::collection(User::query()->with('avatar')->paginate());
}
}
namespace App\Http\Resources;
use Netsells\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'avatar_url' => $this->avatar->url,
];
}
}
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Netsells\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function preloads()
{
return $this->preload('avatar');
}
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'avatar_url' => $this->avatar->url,
];
}
}
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Netsells\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'avatar_url' => $this->use('avatar', fn ($avatar) => $avatar->url),
];
}
}
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Netsells\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'avatar' => $this->one('avatar', AvatarResource::class),
];
}
}
class AvatarResource extends JsonResource
{
public function preloads()
{
return $this->preload('file');
}
public function toArray(): array
{
return [
'id' => $this->id,
'width' => $this->width,
'height' => $this->height,
'url' => $this->file->s3Url(),
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.