Setting up your Data Model in Laravel

Tutorials

September 20th, 2022

Setting up your Data Model in Laravel

The data model is one of the most important parts of any Laravel application. Many systems will be designed around this data model, so it is typically one of the first things we approach during development. Some of us have been doing this for years and have a good idea of how to approach this - while others may not be used to it yet. I learned about data modeling before I knew there was such thing as a framework, designing my data model in CREATE TABLE statements.

In this tutorial, I will walk through how you can approach data modeling in your Laravel application - and some tips on what I find helpful.

This tutorial is part one of an ongoing series where we should have built an entire working production-ready system together from start to finish by the end. In other words, from IDE to a server.

What are we going to build? I am glad you asked. I could do something straightforward here, like a ToDo application or a blog - but you won't learn anything useful from that. Instead, we are going to be building something unique and exciting with quite a few moving parts, which by the end of it all, should be something worthwhile. Recently I went on the hunt for a meeting room booking system, and in all honesty I struggled to find one. So we will be building an open source one in Laravel. The purpose here is to build something in a way that we would expect in a production environment while at the same time offering something for free.

What is going to be included within this meeting room management platform? This isn't going to be a SaaS style application where anyone can sign up and use it, this will be something you download and run yourself. It is crucial at this stage to think about these decisions as it informs much of our data model.

The way we want our application to work is, at first, a configuration process happens where important setup instructions can be gone through. An overall system admin can be invited onto the platform to allow them to invite users and set up the system as required.

Let's first look at our User model, which is not quite the same as the typical Laravel user model. Eventually, we will be refactoring our User model to a package that controls the authentication for us - but for now, we will keep it simple.

Our user model will need an additional column called type, which will be an Enum. We will keep the email verification column to validate users more efficiently in an API-based environment.

The user migration should now look like the following:

1public function up(): void
2{
3 Schema::create('users', function (Blueprint $table): void {
4 $table->id();
5 $table->string('name');
6 $table->string('email')->unique();
7 $table->timestamp('email_verified_at')->nullable();
8 $table->string('password');
9 
10 $table->string('type')->default(Type::ADMIN->value);
11 
12 $table->timestamps();
13 });
14}

As you can see, it is relatively standard for a Laravel application other than the additional column we want to add. From here, we can start looking at the Model Factory we need to create for our users.

1final class UserFactory extends Factory
2{
3 protected $model = User::class;
4 
5 public function definition(): array
6 {
7 return [
8 'name' => $this->faker->name(),
9 'email' => $this->faker->unique()->safeEmail(),
10 'email_verified_at' => now(),
11 'password' => Hash::make(
12 value: 'password',
13 ),
14 'type' => Type::ADMIN,
15 ];
16 }
17 
18 public function unverified(): UserFactory
19 {
20 return $this->state(
21 state: fn (array $attributes) => ['email_verified_at' => null],
22 );
23 }
24 
25 public function type(Type $type): UserFactory
26 {
27 return $this->state(
28 state: fn (array $attributes) => ['type' => $type],
29 );
30 }
31}

We add a default type we want to apply to any user created. However, we also create a helper method so that we can customize the type of user we want to create.

This leads us to the model and the changes we want to apply to it. There are minimal changes to the Eloquent Model, other than an additional fillable column and ensuring that we can cast the type property to our Enum.

1final class User extends Authenticatable implements MustVerifyEmail
2{
3 use HasApiTokens;
4 use HasFactory;
5 use Notifiable;
6 
7 protected $fillable = [
8 'name',
9 'email',
10 'password',
11 'type',
12 ];
13 
14 protected $hidden = [
15 'password',
16 ];
17 
18 protected $casts = [
19 'type' => Type::class,
20 'email_verified_at' => 'datetime',
21 ];
22}

Before we go into more detail, you are probably wondering about the Enum itself and its available options.

1enum Type: string
2{
3 case ADMIN = 'admin';
4 case OFFICE_MANAGER = 'office manager';
5 case STAFF = 'staff';
6}

Our application is going to be made up of the following:

  • Admins; are people who are in charge of configuring the system and managing the system.
  • Office Managers; are people who have the authority to override bookings on the system.
  • Staff; are people on the system who have the ability to book the meeting rooms.

The workflow for this is that Admins will invite Office Managers, who can then start onboarding staff members onto the platform.

Alongside modeling the database representation of the data, we also need a way to understand the data inside the application. We can achieve this by using a Domain Transfer Object, a DTO for short.

We start by going through what the database contains for this data model and then figuring out what is needed across the application. However, we need to be able to create these objects before they exist in the database, too, at times.

1final class User implements DataObjectContract
2{
3 public function __construct(
4 private readonly string $name,
5 private readonly string $email,
6 private readonly Type $type,
7 ) {}
8 
9 public function toArray(): array
10 {
11 return [
12 'name' => $this->name,
13 'email' => $this->email,
14 'type' => $this->type,
15 ];
16 }
17}

For us to create or invite a potential user or know anything about a user, we need access to their name, email, and type. This covers most use cases, as resources are mostly identified through route model binding.

We now have a way to understand the data through the database and the application. This is a process I repeat, in this order, as I build out any Laravel application. It gives me an abstraction from the model to pass around without it being a simple array while forcing a level of type safety on the properties. Sometimes I need access to the properties directly, but not that often, and when I do, I would create an accessor instead of changing the properties' visibility.