Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jordantsap's avatar

how to structure db objects for complex relationships project

I am building a pretty complex project that has both ordering functionality and booking functionality, this means that should have objects/models for listings for product, company, type(company_type, product_type), category model, Accommodation, hotel and room model. Some usefull to understand packages I use their functions are: bumbummen99/shoppingcart spatie/laravel-permission astrotomic/laravel-translatable

Previously the website had only ordering products functions and now i should implement booking feature as well. The old version had some company types and product types as models with seeded 10-20 entries.

So how should be the db structure and relations? This?????: listing: manyToOne('Type_id'), manyToOne('Category_id'),, name, description etc

Type: name, slug, :: Market, Accommodation, Venue, Cafeteria, Hotel, delivery, Hairdressers

Category: name, slug :: Room, Product, Event

Type and Category models may also refer to Companies, Products, Venue, Event or other models, Also Hotels can have many rooms. The difficult question is how to relate Type id with Product model, something like: return $this->hasMany()->where/or when('id', '==', 4)

0 likes
2 replies
LaryAI's avatar
Level 58

Based on the information provided, here is a possible database structure and relationship setup for your project:

  1. Create the following tables:
  • listings: id, type_id, category_id, name, description, ...
  • types: id, name, slug, ...
  • categories: id, name, slug, ...
  • companies: id, name, ...
  • products: id, name, ...
  • venues: id, name, ...
  • events: id, name, ...
  • accommodations: id, name, ...
  • hotels: id, name, ...
  • rooms: id, hotel_id, name, ...
  1. Define the relationships in your models:

In the Listing model:

public function type()
{
    return $this->belongsTo(Type::class);
}

public function category()
{
    return $this->belongsTo(Category::class);
}

In the Type model:

public function listings()
{
    return $this->hasMany(Listing::class);
}

public function companies()
{
    return $this->hasMany(Company::class);
}

public function products()
{
    return $this->hasMany(Product::class);
}

// Add other relationships as needed

In the Category model:

public function listings()
{
    return $this->hasMany(Listing::class);
}

public function companies()
{
    return $this->hasMany(Company::class);
}

public function products()
{
    return $this->hasMany(Product::class);
}

// Add other relationships as needed

In the Hotel model:

public function rooms()
{
    return $this->hasMany(Room::class);
}

In the Room model:

public function hotel()
{
    return $this->belongsTo(Hotel::class);
}
  1. To relate the Type model with the Product model, you can use a pivot table:
  • type_product: type_id, product_id

In the Type model:

public function products()
{
    return $this->belongsToMany(Product::class, 'type_product');
}

In the Product model:

public function types()
{
    return $this->belongsToMany(Type::class, 'type_product');
}

This setup allows you to define the relationships between the different models and handle the complex relationships in your project. You can modify the relationships and table structures based on your specific requirements.

1 like
jordantsap's avatar

@LaryAI Please provide me an example for the controllers for creating and listing of product etc that related to listing model

Please or to participate in this conversation.