I have a table store, and store has many libraries, in library I have foreign key of store store_id
.
Store table
id(PK)
L
Try this one. It works. Add this to your model.
Library model
public function store()
{
return $this->belongsTo(Store::class, 'store_id', 'id');
}
Store model
public function libraries()
{
return $this->hasMany(Library::class);
}
example code.
$store = Store::find(1);
dd($store->libraries);
Because in this case a store has many libraries, the Store
model has a libraries()
function. Refer to last line of James' answer for more information on this standard.