我需要從SQL與雄辯後得到查詢,創建groupBy('date')
和sortBy('price')
與collect
在Laravel 5.3
。如何在groupBy中使用sortBy在Laravel 5.3中收集?
在IndexController
:
$flights = Flights::GetFlights($fromCity, $toCity, $fromDate);
$collection = collect($flights);
$sortFlight = $collection->groupBy('date')->sortBy('price');
在Model
:
public function scopeGetFlights($query, $fromCity, $toCity, $fromDate)
{
// Between dates
$beforeDate = date('Y-m-d', strtotime($fromDate . '- 10 days'));
$afterDate = date('Y-m-d', strtotime($fromDate . '+ 10 days'));
$join = $query
-> join('airlines', 'airlines.pana', 'flights.airline')
-> where('flights.from_city', $fromCity)
-> where('flights.to_city', $toCity)
-> whereBetween('flights.date', [$beforeDate, $afterDate])
-> get([
'flights.*',
'airlines.pana as pana',
'airlines.name as airlineName'
]);
return $join;
}
然後Print_r($sortFlight)
,打印groupBy('date')
後是工作 但sortBy('price')
不起作用!!!!
我的問題在哪裏?
@Punit嗨..,如何使用'sortBy( 'flights.price')'的收集? – mySun