Laravel: N to N relationship (mysql) using Eloquent ORM

I have 4 tables:

// Table countries
+----+------+
| Id | Name |
+----+------+
|  1 | USA  |
|  2 | GB   |
+----+------+

// Table platforms
+----+---------+
| Id |  Name   |
+----+---------+
|  1 | Windows |
|  2 | Linux   |
+----+---------+

// Table users
+----+-------+------------+-------------+
| Id | Name  | country_id | platform_id |
+----+-------+------------+-------------+
|  1 | Admin |          1 |           1 |
|  2 | Test  |          2 |           1 |
+----+-------+------------+-------------+

// Table posts
+----+-----------+------------+-------------+---------+
| Id |   Title   | country_id | platform_id | user_id |
+----+-----------+------------+-------------+---------+
|  1 | TestPost1 |          2 |           1 | 1       |
|  2 | TestPost2 |          2 |           2 | null    |
+----+-----------+------------+-------------+---------+

      

The database must be able to implement the following relationships:

  • User (N) ↔ (N) Platform
  • User (N) ↔ (N) Country
  • User (0..1) ↔ (N) Message
  • Message (N) ↔ (N) Country
  • Message (N) ↔ (1) Platform

So now I tried to implement this relationship following the Laravel Eloquent ORM documentation :

  // Country.php
  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  // Platform.php
  public function users()
  {
      return $this->belongsToMany('App\User');
  }

  public function posts()
  {
      return $this->belongsToMany('App\Post');
  }

  // User.php
  public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function countries()
    {
        return $this->hasMany('App\Country');
    }

    public function platforms()
    {
          return $this->hasMany('App\Platform');
    }

  // Post.php
  public function countries()
  {
      return $this->hasMany('App\Country');
  }

  public function platforms()
  {
      return $this->hasMany('App\Comment');
  }

  public function user()
  {
      return $this->belongsTo('App\User', 'user_id');
  }

      

But now I am confused as I thought the way to implement NN relationships in mysql is to add a third table to the db, for example:

// Table CountryUserRelations to implement User (N) <-> (N) Country
+----+------------+---------+
| Id | country_id | user_id |
+----+------------+---------+
|  1 |          1 |       1 |
|  2 |          2 |       1 |
|  3 |          1 |       2 |
|  4 |          2 |       2 |
+----+------------+---------+

      

But how does the Eloquent ORM handle rules inside my model? Will it support N to N relationships without adding a relationship table? Or am I missing something or an incomprehensible concept "Eloquent relationship with ORM"?

+3


source to share


1 answer


I just joined stackoverflow, so I don't have enough comments to comment, so I'll leave asnwer here.

First, correct your definition of relationships.

in the user model :( you have a bug here)

public function countries(){
    return $this->belongsToMany(Country::class);
}

      

and your country Model:



public function users(){
    return $this->belongsToMany(User::class);
}

      

seconds you need to create a table country_user

using:

php artisan make:migration create_country_user_table

      

after that you need to fill the table:

Schema::create('country_user', function (Blueprint $table){
    $table->increments('id');
    $table->unsignedInteger('country_id');
    $table->unsignedInteger('user_id');
    $table->foreign('country_id')->references('id')->on('countries');
    $table->foreign('user_id')->references('id')->on('users');
}

      

+1


source







All Articles