Init
This commit is contained in:
36
apps/backend/app/Data/UserData.php
Normal file
36
apps/backend/app/Data/UserData.php
Normal 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user