Issue
I have 2 models(market,price_item), i want return last prices for each item (model 2) for 1 market (model 1).
i have in my model 1:
public function prices()
{
return $this->hasMany(MarketPrices::class, self::IDMarket)->groupBy(['item']);
}
This solution give me price for each item but not last price. I tried orderBydesc but not work.
Thanks you for your help
Solution
First of all this is not how you create a relation A "one-to-many" relationship is used to define relationships where a single model owns any amount of other models. For example, a blog post may have an infinite number of comments. Like all other Eloquent relationships, one-to-many relationships are defined by placing a function on your Eloquent model:
public function prices()
{
return $this->hasMany('Models/MarketPrices', 'id');
}
call it like
$price = Yourmodel::find(1);
dd($price->prices); //return the 1 model connected
Answered By - Wim Pruiksma Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.