What is DTO and how to use it in a Laravel application?

EL OUFIR Hatim
2 min readFeb 28, 2023

--

DTO stands for Data Transfer Object, which is a design pattern used to transfer data between different layers of an application. In a DTO, data is encapsulated into an object, which can then be easily passed between different parts of the application.

In a Laravel application, DTOs can be used to transfer data between controllers and services, or between services and repositories. This helps to keep the business logic separate from the presentation logic, and makes the application more modular and maintainable.

To use DTOs in a Laravel application, you can create a new class for each DTO that you need. For example, if you have a User model with properties like name, email, and password, you could create a UserDTO class like this:

namespace App\DTO;

class UserDTO
{
public string $name;
public string $email;
public string $password;

public function __construct(string $name, string $email, string $password)
{
$this->name = $name;
$this->email = $email;
$this->password = $password;
}
}

You can then use this DTO in your controller, for example, to create a new user:

namespace App\Http\Controllers;

use App\DTO\UserDTO;
use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
private UserService $userService;

public function __construct(UserService $userService)
{
$this->userService = $userService;
}

public function createUser(Request $request)
{
$userDTO = new UserDTO(
$request->input('name'),
$request->input('email'),
$request->input('password')
);

$user = $this->userService->createUser($userDTO);

return response()->json($user);
}
}

In the above example, the createUser method in the UserController receives a Request object from the client. It then creates a new UserDTO object using the input data from the request, and passes this DTO to the createUser method of the UserService. The UserService can then use the DTO to create a new user in the database, without having to know anything about the request or the controller.

Using DTOs in this way can make your Laravel application more modular, maintainable, and testable.

Here is an example of how to link your DTO to your Model and then save it into your database:

namespace App\Services;

use App\DTO\UserDTO;
use App\Models\User;

class UserService
{
public function createUser(UserDTO $userDTO): User
{
$user = new User();
$user->name = $userDTO->name;
$user->email = $userDTO->email;
$user->password = $userDTO->password;
$user->save();

return $user;
}
}

In the createUser method, a new User model is created and its properties are set using the values from the UserDTO. The model is then saved to the database using the save() method.

Of course, you can use a UserRepository to do the save logic, so you will have a complete architecture using Model> DTO> Repository> Service> Controller architecture for me is the best and I am using it almost in every complex Laravel project.

--

--

EL OUFIR Hatim

I’m a Full Stack developer who is passionate about making open-source more accessible and building community.