hasMany, Copyright To, ToMany, параметры с пользовательскими именами? - PullRequest
0 голосов
/ 13 марта 2019

У меня есть эти таблицы БД:

author (atr_id pk)                              // model : Author
category (ctg_id pk)                            // model : Category
post (pst_id pk, pst_atr_id fk)                 // model : Post
post_categories (pct_pst_id fk, pct_ctg_id fk)  // pivot : PostCategories 

как определить параметры hasMany, sharesTo, assignToMany с пользовательскими именами?

Модель: Автор

public function getPosts()
{
    return $this->hasMany('App\Post', ?, ?);
}

Модель: Пост

public function getAuthor()
{
    return $this->belongsTo('App\Author', ?, ?);
}

public function getCategories()
{
    return $this->belongsToMany('App\Category', ?, ?, ?);
}

Модель: Категория

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

Ответы [ 3 ]

2 голосов
/ 13 марта 2019

Модель: Автор

<?php

public function getPosts(){
     return $this->hasMany('App\Post','pst_atr_id');// author table's foreign key in Post table.
}

Модель: Пост

<?php

 public function getAuthor(){
     return $this->belongsTo('App\Author','pst_atr_id','atr_id'); // first is foreign key of author table in this current table and second is primary key name of author table.
 }

 public function getCategories(){
     return $this->belongsToMany('App\Category','post_categories','pct_pst_id ','pct_ctg_id'); /* 'post_categories' is the pivot table. Rest 2 parameters- first is foreign key present in post_categories of the current model, second is foreign key present in post_categories of the model we are relating with*/
 }

Модель: Категория

<?php

 public function getPosts(){
      return $this->belongsToMany('App\Post','post_categories ','pct_ctg_id','pct_pst_id');

    /* 'post_categories' is the pivot table. Rest 2 parameters- first is foreign key present in post_categories of the current model, second is foreign key present in post_categories 
     of the model we are relating with*/
 }
0 голосов
/ 13 марта 2019
Author   : $this->hasMany('App\Post', 'pst_atr_id', 'atr_id');

Post     : $this->belongsTo('App\Author', 'pst_atr_id', 'atr_id');

Post     : $this->belongsToMany('App\Category', 'post_categories', 'pct_pst_id', 'pct_ctg_id');

Category : $this->belongsToMany('App\Post', 'post_categories', 'pct_ctg_id' 'pct_pst_id');
0 голосов
/ 13 марта 2019

Модель: Автор

public function getPosts()
{
    return $this->hasMany('App\Post', 'pst_id', 'atr_id');
}

Модель: Пост

public function getAuthor()
{
    return $this->belongsTo('App\Author', 'pst_atr_id', 'pst_id');
}

public function getCategories()
{
    return $this->belongsToMany('App\Category', 'post_categories', 'pct_pst_id', 'pct_ctg_id');
}

Модель: Категория

public function getPosts()
{
    return $this->belongsToMany('App\Post', 'post_categories', 'pct_ctg_id', 'pct_pst_id');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...