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

sersi's avatar
Level 1

more than 3 tables relationship

Hello, I'm new to Laravel and I watched some videos about eloquent relationships but am still confused about the relationship with more than 3 tables. Please help me

  • in my case I tried to build it system with store movement for every item and make maintenance for items with 3 store movement (issued, received, returned) and every movement related to the department with the employee, and item at this point everything is done.

  • in case this item needs maintenance

    • I made 4 table
      1. maintenances table with these columns

        • id
        • item_id
        • store_id
        • status_after_maintenance
        • maintenance_date
      2. repairs table with these columns

        • id
        • repair_name like (hardware and software)
      3. tools table with these columns

        • id
        • repair_id ex: (1) refer to hardware
        • tool_name ex: (hard disk)
      4. maintenance_repair_tool table as a pivot with these columns

        • id

        • maintenance_id

        • repair_id

        • tool_id

        • I will pass this data (repair_id's and tool_id's) from blade file while store the maintenance

/*Maintenance  Model*/
class Maintenance extends Model
{
  
    public function repairs()
    {
        return $this->belongsToMany(Repair::class, 'maintenance_repair_tool','maintenance_id','repair_id','id','id');
    }
  public function tools()
    {
        return $this->belongsToMany(Tool::class, 'maintenance_repair_tool','maintenance_id','tool_id','id','id');
    }
}
/*Repair  Model*/
class Repair extends Model
{

   public function all_tools()
    {
        return $this->hasMany(Tool::class, 'repair_id', 'id');
    }
    public function maintenances()
    {
        return $this->belongsToMany(Maintenance::class, 'maintenance_repair_tool','repair_id','maintenance_id','id','id');
    }

    public function tools()
    {
        return $this->belongsToMany(Tool::class, 'maintenance_repair_tool','repair_id','tool_id','id','id');
    }
}
/*Tool  Model*/
class Tool extends Model
{

    public function repair(){
      return $this->hasOne(Repair::class,'id','repair_id');
   }
    public function maintenances()
{
        return $this->belongsToMany(Maintenance::class, 'maintenance_repair_tool','tool_id','maintenance_id','id','id');
    }
public function repairs(){
        return $this->belongsToMany(Repair::class, 'maintenance_repair_tool','tool_id','repair_id','id','id');
    }
}
0 likes
6 replies
Sinnbeck's avatar

First let's start with the basics. Maintenence has a column named item_id. That means it belongsTo an item. Not hasOne. Same with store.

Secondly your item::class has a lowercase i. Classes start with an uppercase letter

I'm also quite confused as to why you have the same table twice? Maintenance x2. Table names are the multiple version of the class. So Item class = items

sersi's avatar
Level 1

@Sinnbeck thx for advance

  • about this point -> item::class has a lowercase i. Classes start with an uppercase letter (done)

  • about this point - > Maintenance x2. Table names are the multiple version of the class.

    • this typo error sorry (the table name is maintenance_repair_tool and this is a pivot table for me )
  • about the relation between items table and maintenances table I looked at the maintenance model and my idea each maintenance has one item in my case EX: the item is PC and the current status for this pc is returned (in the warehouse) this pc is already issued to the employee before and with some reason is returned to (IT) department to make another maintenance so I need to store item_id and store_id with the movement name also in maintenances table

  • I will edit my question now and shortly explain my case

Sinnbeck's avatar

@sersi cool. Regarding hasOne vs belongsTo. It's just how relations work :) the table with the foreign key (sometable_id) always belongs to the other. And on the table that is pointed to, it's either hasOne or hasMany. Notice how the naming are close to the same here. It's because it in theory is the same relationship. Only difference is that laravel returns 1 row/model vs many rows/models

sersi's avatar
Level 1

@Sinnbeck - about the relation between items table and maintenances table I looked at the maintenance model and my idea each maintenance has one item in my case EX: the item is PC and the current status for this pc is returned (in the warehouse) this pc is already issued to the employee before and with some reason is returned to (IT) department to make another maintenance so I need to store item_id and store_id with the movement name also in maintenances table

Snapey's avatar

perhaps it might help to think of the pivot actually as its own model

Perhaps Job

a Job would be a piece of work for an item, using a tool, and satisfying a maintenance or repair request ?

It then makes sense why this model would have three foreign keys

Please or to participate in this conversation.