Compare commits

..

No commits in common. "6c0309b9b225e2c451d77fc000e74e4aa85d788d" and "71c4fd7bb9f2aabde2ae6f24a32238f87a20c80f" have entirely different histories.

6 changed files with 438 additions and 426 deletions

12
app/Data/RoleData.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace App\Data;
use Spatie\LaravelData\Data;
class RoleData extends Data
{
public function __construct(
public string $name,
) {}
}

36
app/Data/UserData.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App\Data;
use App\Models\User;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Lazy;
use Spatie\LaravelData\Optional;
class UserData extends Data
{
public function __construct(
public int $id,
public string $name,
public string $email,
#[DataCollectionOf(RoleData::class)]
public Lazy|DataCollection $roles,
public string $created_at,
public string $updated_at,
) {}
public static function fromModel(User $user): self
{
return new self(
$user->id,
$user->name,
$user->email,
Lazy::whenLoaded('roles',$user, fn() => RoleData::collection($user->roles)),
$user->created_at,
$user->updated_at,
);
}
}

View File

@ -12,8 +12,8 @@
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8",
"pusher/pusher-php-server": "^7.2",
"spatie/laravel-permission": "^6.0",
"spatie/laravel-query-builder": "^5.6"
"spatie/laravel-data": "^3.9",
"spatie/laravel-permission": "^6.0"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",

763
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,46 +0,0 @@
<?php
return [
/*
* By default the package will use the `include`, `filter`, `sort`
* and `fields` query parameters as described in the readme.
*
* You can customize these query string parameters here.
*/
'parameters' => [
'include' => 'include',
'filter' => 'filter',
'sort' => 'sort',
'fields' => 'fields',
'append' => 'append',
],
/*
* Related model counts are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsCount
*/
'count_suffix' => 'Count',
/*
* Related model exists are included using the relationship name suffixed with this string.
* For example: GET /users?include=postsExists
*/
'exists_suffix' => 'Exists',
/*
* By default the package will throw an `InvalidFilterQuery` exception when a filter in the
* URL is not allowed in the `allowedFilters()` method.
*/
'disable_invalid_filter_query_exception' => false,
/*
* By default the package will throw an `InvalidSortQuery` exception when a sort in the
* URL is not allowed in the `allowedSorts()` method.
*/
'disable_invalid_sort_query_exception' => false,
];

View File

@ -1,5 +1,6 @@
<?php
use App\Data\UserData;
use App\Http\Resources\UserResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@ -16,5 +17,5 @@ use Illuminate\Support\Facades\Route;
*/
Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
return new UserResource($request->user()->load('roles'));
return UserData::from($request->user()->load('roles'));
});