RuinSain's avatar

Building an e-commerce shop can be hard. Whats the best practice DB wise?

I bet there are lots of this kinda stuff. But tell you the truth, navigating among the numerous options is the worst! So I'm asking you all, what can be a good approach to design a database, with the relations and all in order to make such a site? Actually, this can even be a good series here on laracasts, but until this happens... what do you think?

I made a few models. Item, Cart, Category, and User. How can i set relationship between them in the most good way? Making a pivot table to an item and a cart? And then a cart and a user? Or everything in one table? You see what I'm saying? Lots of ways. P.S I know there are packages for this kindsa stuff but i want to learn best practices in these situations.

Let's discuss this. Thanks!

0 likes
6 replies
Nakov's avatar

So this will be my setup:

User has many Carts => one to many

Items can belong to many Categories => Many to Many

Items can belong to many Carts => Many to Many

RuinSain's avatar

How can a user have many carts? Maybe we can add an Order model? And a user can have many orders? @nakov

Nakov's avatar

@ruinsain yes, an order is a better term. That's what I meant as well. Because you fill in the cart, you make the order, and then you can empty the cart and make another order.

So yeah, a user can have many orders. The cart is just a virtual object, you don't store that in the database.

RuinSain's avatar

So how, in practice should i actually make the migrations and model relations? Because i am actually confused. @nakov

Nakov's avatar

@ruinsain I will share the documentation here as that's the place were you should refer to for better explanation (https://laravel.com/docs/master/eloquent-relationships). But I will also share with you my approach :)

User.php

public function orders()
{
    return $this->hasMany(Order::class);
}

So the orders table migration will be something like this:

Schema::create('orders', function(Blueprint $table) {
   $table->bigInteger('id');

    // your other order fields
   // the user relationship
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}); 

And an example of a many to many migration will be :

Schema::create('category_item', function(Blueprint $table)
{
    $table->bigInteger('id');

    $table->unsignedBigInteger('order_id');
    $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');

    $table->unsignedBigInteger('item_id');
    $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
});

Then the models should be this way:

// Category.php

public function items()
{
    return $this->belongsToMany(Item::class);
}

// Item.php

public function categories()
{
    return $this->belongsToMany(Category::class);
}

NOTE

Check how I named the many to many table to be category_item and that's a laravel convention, so it starts alphabetically to connect two tables, this way you won't need to add extra parameters to the belongsToMany relationship.

I hope you find this helpful, but please read the documentation it will help you more :)

If you have more questions I'll be happy to help.

RuinSain's avatar

I will review and try out your approach. Thanks so much.

Please or to participate in this conversation.