2017-06-15 73 views
1

抱歉,新手问题。我想从表格特定的列中获取,按日期排序,只提取150行。我尝试了很多组合。但我不明白了应该如何工作的:Laravel Eloquent SELECT sortBy with limit 150 rows

public function get() 
    { 
     Excel::create('Bids-' . date("Y/m/d"), function($excel) { 
      $excel->sheet('Favourites', function($sheet) { 
       $comments = Comment::select('auction_name','bid','lot_date','company', 'model_name_en', 'body','grade_en', 'mileage_num', 'model_year_en', 'start_price_en')->sortBy("lot_date", 'desc')->take(150)->get(); 
       $sheet->fromModel($comments); 
      }); 
     })->download('xls'); 
    } 

请,可有人给我解释一下什么即时做错了,为什么sortBy不用想工作吗?

回答

2
$comments = Comment::select('auction_name','bid','lot_date','company', 'model_name_en', 'body','grade_en', 'mileage_num', 'model_year_en', 'start_price_en')->orderBy("lot_date", 'desc')->take(150)->get(); 

通知使用orderBy()而不是sortBy()

+0

请提供适当的格式和解释你的答案 – Ray

+0

它的工作实际上,问题是,我需要在sortBy'的'代替使用'orderBy' –

0

其实我找到一个解决方案,但我仍然很难得到它,为什么它没有在以前的工作。

这是解决方案:

Excel::create('Bids-' . date("Y/m/d"), function($excel) { 
      $excel->sheet('Favourites', function($sheet) { 

       $comments = Comment::select('auction_name','bid','lot_date','company', 'model_name_en', 'body','grade_en', 'mileage_num', 'model_year_en', 'start_price_en')->take(150)->get(); 
       $result = $comments->sortByDesc('lot_date'); 
       $sheet->fromModel($result); 
      }); 
     })->download('xls');