Dmitry Malys
Dmitry Malys

Reputation: 1323

Laravel Eloquent SELECT sortBy with limit 150 rows

sorry for newbie question. I want to get from table specific columns, order them by date and extract only 150 rows. I tried a lot of combinations. But i dont get the point how should it work:

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');
    }

Please, can some one explain me what im doing wrong, why sortBy dont want to work?

Upvotes: 1

Views: 240

Answers (2)

赵加兴
赵加兴

Reputation: 34

$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();

notice usage of orderBy() instead of sortBy().

Upvotes: 2

Dmitry Malys
Dmitry Malys

Reputation: 1323

Actually i find a solution, but i still hardly get it, why it didn't work in before.

This is the solution:

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');

Upvotes: 0

Related Questions