php - Laravel: N to N (mysql) relation using Eloquent ORM -


i got 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 should able implement following relations:

  • user (n) <-> (n) platform
  • user (n) <-> (n) country
  • user (0..1) <-> (n) post
  • post (n) <-> (n) country
  • post (n) <-> (1) platform

so tried implement these relations following 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 confused, thought way implement n n relations in mysql add third table db, example that:

// table countryuserrelations implement user (n) <-> (n) country +----+------------+---------+ | id | country_id | user_id | +----+------------+---------+ |  1 |          1 |       1 | |  2 |          2 |       1 | |  3 |          1 |       2 | |  4 |          2 |       2 | +----+------------+---------+ 

but how eloquent orm handle rules inside model? keep n n relations without having add relations table? or missing or misunderstanding eloquent orm relations concept?

i joined stackoverflow not have enough credit comment leave asnwer here.

first of please correct relationship definition.

in user model:( have mistake here)

public function countries(){     return $this->belongstomany(country::class); } 

and in country model:

public function users(){     return $this->belongstomany(user::class); } 

second need create country_user table using:

php artisan make:migration create_country_user_table 

after need complete 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'); } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -