PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How my model Laravel could be return me all prices for each item only last date in hasMany?

 October 21, 2022     eloquent, has-many, laravel, model     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing