Store table:store_id (PK)
Library table:library_id (PK) library_fk_store_id (FK)
Store model:
public function libraries()
{
return $this->hasMany(Library::class, 'library_fk_store_id','library_id');
}
Library model:
public function store()
{
return $this->belongsTo(Store::class, 'library_fk_store_id', 'store_id');
}
- Foreign keys are identical in both methods:
`library_fk_store_id`
- Local/Owner keys are the ids from the opposite model/table:
`Store model -> library_id` `Library model -> store_id`
- Local/Owner keys has the same name from the method names:
`libraries() -> library_id` `store() -> store_id`
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
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');
Leave a Reply