To illustrate my point here is some sample code:
// Models/User.php
class User
{
protected $guarded = [];
}
// Dto/User.php
class User extends Spatie\DataTransferObject\DataTransferObject
{
public readonly int $id
public string $username;
public string $password;
}
// Repositories/UserRepository.php
use Dto/User;
interface UserRepository
{
public function getById(int $id): ?User;
}
// Repositories/UserRepositoryEloquent.php
use Dto/User;
use Models/User as UserModel;
class UserRepositoryEloquent implements UserRepository
{
public function getById(int $id): ?User
{
$userModel = User::query()->where('id', $id)->first();
return $userModel !== null ? $this->convertToDto($userModel) : null;
}
protected function convertToDto(UserModel $userModel): User
{
return new User($userModel->toArray());
}
}
Now imagine that I have a UserSecuritySetting should I just give up on eloquent relations and foreign keys and just have a unsigned int column user_id that I will use to fetch security setting for user with specific id? If I do this, should I hydrate my User DTO with an attribute like securitySetting and attach the UserSecuritySetting DTO to it or is it clean enough to use them seperately?