skip to Main Content

In Laravel can I create oneToMany relations with junction tables instead of belongsToMany?

Tables:

  • Users: id, name, etc

  • Table1: id, name, etc

  • Table2: id, name, etc

  • Table3: id, table1_id, table2_id, currentActive, lastActive

    [junction table with pivot columns]
  • Table4: id, table3_id, user_id, actionPending

    [another junction table with first junction table id and some other table id with some pivot columns]

Can I create relations like this?

Table1 hasMany Table3 instead of belongsToMany
Table2 hasMany Table3 instead of belongsToMany
Table3 hasMany Table4 instead of belongsToMany
Table4 belongsTo Table3 and Users
Users hasMany Table4

2

Answers


  1. It is not necessary to notate these relationships in the model. The reason we write those relationships is that we can build queries easier.

    For example: you have a User model which has a one-to-many relationship with a Post model, you can define the relationships in the models like this:

    User.php

    public function posts(): HasMany
    {
      return $this->hasMany(Post::class)
    }
    

    Post.php

    public function user(): BelongsTo
    {
      return $this->belongsTo(User::class)
    }
    

    Then in your controllers you can easily get all the posts belonging to a user like this:

    $user = Auth::user(); // retrieve the authenticated user
    $posts = $user->posts(); // retrieve all the posts related to the user.
    

    Otherwise you would have to do it like this:

     $user = Auth::user(); // retrieve the authenticated user
     $posts = Post::where('user_id', $user->id); // retrieving all the posts
    

    This was ofcourse a simple example, but you can already see the power of Laravel’s Eloquent relationships. Here you can read more about defining relationships and querying with Eloquent relationships:
    https://laravel.com/docs/10.x/eloquent-relationships

    So to summarize: you don’t have to define the relationships in your models. The relationship is the fact that you have a foreign key.
    The reason why it is recommended to define the relationships in your models is so that you can take advantage of Laravel’s power.

    I hope you understand it better now.

    Login or Signup to reply.
  2. Yes, in Laravel, you can create one-to-many relationships with junction tables using hasMany through custom intermediate tables. This can be achieved using custom pivot models to define the relationships and customize the intermediate table columns. Here’s how you can define the relationships based on the provided tables:

    Table1 hasMany Table3 instead of belongsToMany:

    In your Table1 model, you can define a hasMany relationship with Table3:

    // Table1.php
    public function table3Entries()
    {
        return $this->hasMany(Table3::class, 'table1_id');
    }
    

    Table2 hasMany Table3 instead of belongsToMany:

    Similarly, in your Table2 model, you can define a hasMany relationship with Table3:

    // Table2.php
    public function table3Entries()
    {
        return $this->hasMany(Table3::class, 'table2_id');
    }
    

    Table3 hasMany Table4 instead of belongsToMany:

    You can use the hasMany relationship to define the relationship between Table3 and Table4. Additionally, you can define a belongsTo relationship with the Users table:

    // Table3.php
    public function table4Entries()
    {
        return $this->hasMany(Table4::class, 'table3_id');
    }
    
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    

    Table4 belongsTo Table3 and Users:

    In the Table4 model, you can define the belongsTo relationships with Table3 and Users:

    // Table4.php
    public function table3()
    {
        return $this->belongsTo(Table3::class, 'table3_id');
    }
    
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    

    Users hasMany Table4:

    Finally, in the User model, you can define the hasMany relationship with Table4:

    // User.php
    public function table4Entries()
    {
        return $this->hasMany(Table4::class);
    }
    

    By defining the relationships in this manner, you can establish the desired one-to-many relationships using the junction tables in Laravel.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search