Answer the question
In order to leave comments, you need to log in
Why does the Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::stores() error occur?
There are two models: warehouse and branch.
A branch can have many warehouses. Accordingly, the Branch in the model has
public function stores(){
return $this->hasMany('App\Store');
}
public function branch()
{
return $this->belongsTo('App\Branch');
}
public function getStoresOfParentBranch(){
return $this->branch()->stores();
}
Answer the question
In order to leave comments, you need to log in
Because it $this->branch()
issues \Illuminate\Database\Eloquent\Relations\BelongsTo
, not the model
So no one causes a relationship. There are a lot of problems with this getStoresOfParentBranch method. From thousands of identical queries to the database to errors.
It's right to do so.
If the relation is called once, then we write immediately in the controller / template.
$store->branch->stores;
But NOT in the model.
If cycles or collections of json resources are planned, or simply > 1 relationship calls, then we first chain through with
Store::with('branch.stores')
->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question