dimanche 30 octobre 2016

laravel get last comment from each post

I have 2 tables, Items and Bids.

Items hasMany(Bid);

Table items

  • id-1
  • id-2
  • id-3

Table bids

  • id-1 | 3
  • id-1 | 4
  • id-2 | 2
  • id-2 | 5
  • id-3 | 4
  • id-3 | 6

and now I want to show the highest bid of each item.

and here is my code

    $data = PostItem::with(['bids' => function($query){
                    $query->orderBy('new_bid','desc')->get();
                }])
                ->get();

but the problem is it will takes all bids.

but if i use this code

    $data = PostItem::with(['bids' => function($query){
                    $query->orderBy('new_bid','desc')->take(1);
                }])
                ->get();

it will takes only 1 latest bid, do not take the latest bid of each item.

How to do it properly?

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire