Laravel relational database models

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

By now, we created a database in Laravel, and made our first seeder. Another great next step is to look into relational models.
So far, we have created a book model. Let's say we are going to introduce a category. Each book will belong to one category, and a category can have many books.
Thinking in this way will help you determine which connection you will need. Laravel does a super good way of documenting these.
First, let's start by creating a model and database by running the following command.
php artisan make:model Category --migration
This will make a category model and create the migration for it.
Modify the migration to look like this.
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Then we also need to add the link to our book table. Now there are two ways of doing this.
Generally, I like to keep my migrations clean if there is no real-life data in them. If that is the case, do write a specific new migration.
In this case, since we are creating the relationship later, we can include the book change. This is what the total migration will look like.
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Schema::table('books', function (Blueprint $table) {
$table->unsignedBigInteger('category_id')->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
});
That adds a relation from the book database to the category database.
Don't forget the add the down function!
Schema::dropIfExists('categories');
Schema::table('book', function (Blueprint $table) {
$table->dropForeign(['category_id']);
$table->dropColumn('category_id');
});
It is very cool, and the database will be able to click through, but the real magic now comes in the models.
Let's start by altering the book model. As mentioned in the intro, one book will belong to one category.
class Book extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
}
That tells the book it belongs to a category. Laravel will do all of the magic for us from here.
Now on the category side, we have to say one category can have many books.
class Category extends Model
{
public function books()
{
return $this->hasMany(Book::class);
}
}
And that is literally all you need to make relations between models.
Let's run a new migration since we altered our existing migration.
php artisan migrate:fresh --seed
I've added some demo data so you can see the relations work.

Can you create a seeder for the category part? You can look at how I created the Book seeder in Laravel.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter