Compare commits
2 Commits
71c4fd7bb9
...
6c0309b9b2
| Author | SHA1 | Date |
|---|---|---|
|
|
6c0309b9b2 | |
|
|
2db222d6ef |
|
|
@ -1,12 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Data;
|
|
||||||
|
|
||||||
use Spatie\LaravelData\Data;
|
|
||||||
|
|
||||||
class RoleData extends Data
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
public string $name,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
<?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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -12,8 +12,8 @@
|
||||||
"laravel/sanctum": "^3.3",
|
"laravel/sanctum": "^3.3",
|
||||||
"laravel/tinker": "^2.8",
|
"laravel/tinker": "^2.8",
|
||||||
"pusher/pusher-php-server": "^7.2",
|
"pusher/pusher-php-server": "^7.2",
|
||||||
"spatie/laravel-data": "^3.9",
|
"spatie/laravel-permission": "^6.0",
|
||||||
"spatie/laravel-permission": "^6.0"
|
"spatie/laravel-query-builder": "^5.6"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"fakerphp/faker": "^1.9.1",
|
"fakerphp/faker": "^1.9.1",
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?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,
|
||||||
|
];
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Data\UserData;
|
|
||||||
use App\Http\Resources\UserResource;
|
use App\Http\Resources\UserResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
@ -17,5 +16,5 @@ use Illuminate\Support\Facades\Route;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
|
Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
|
||||||
return UserData::from($request->user()->load('roles'));
|
return new UserResource($request->user()->load('roles'));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue