mardi 24 novembre 2015

Laravel 5.1 query using 2 tables

I have 2 tables: items and item_details.

items:

id | name | shop_id

item_details:

id | item_id | price | delivery | ...

I need to get all items with unique names with highest price. In other words I need to get a max price value from table 2 for each unique name in table 1.

I tried to use this query, but it returns only 1 row:

$item_details = \DB::table('item_details')
->join('items', 'items.id', '=', 'item_details.item_id')
->where('item_details.price', \DB::raw("(select max(`price`) from item_details)"))
->get();

When I use $items = Item::all(); and $item_details = ItemDetails::all();

                     <tbody>
                        @if (isset($item_details) && count($item_details))
                        @foreach($item_details as $detail)
                        <tr>
                            <th scope="row" >{{ $detail->item->name}}</th>
                            <td>{{ $detail->price}}</td>
                            <td>{{ $detail->delivery}}
                        </tr>
                        @endforeach
                        @else
                        @endif  
                    </tbody>

I have repetition of names, but I need unique names with highest price value for each. Mb someone knows how to make it easier.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire