2015-04-07 20 views
1

我在Laravel新手,我工作的一個項目上Laravel 5laravel 5:最新() - > get()方法不能提供準確的結果

我試圖讓導致基於created_at降序。但是我只能得到升序的結果。

這是我的控制器文件

<?php namespace App\Http\Controllers; 

use App\Position; 
use App\Http\Requests; 
use App\Http\Controllers\Controller; 

use Illuminate\Http\Request; 

class PositionController extends Controller { 
/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    $positions = Position::latest()->get(); 
    print_r($positions); 

} 

這是我的模型文件

<?php namespace App; 

    use Illuminate\Database\Eloquent\Model; 

    class Position extends Model { 

    // 

    } 

請諮詢我錯了我在做什麼?

在此先感謝

回答

1

你試過喜歡這個?:

public function index() 
{ 
    $positions = Position::orderBy('created_at', 'ASC')->get(); 
    print_r($positions); 

} 
+0

$ positions = Position :: orderBy('created_at','DSC') - > get();這給了我想要的結果,但我想知道爲什麼latest() - > get()不起作用 – Neo

1
public function index() 
{ 
    $positions = Position::oldest()->get(); 
    print_r($positions); 

} 

使用這兩種方法,你會得到相同的查詢字符串,所以它是非常奇怪的,你得到不同的輸出。

>>> App\Category::oldest()->toSql() 
=> "select * from `categories` where `categories`.`deleted_at` is null order by `created_at` asc" 

>>> App\Category::orderBy('created_at','ASC')->toSql() 
=> "select * from `categories` where `categories`.`deleted_at` is null order by `created_at` asc" 

>>> App\Category::orderBy('created_at','DESC')->toSql() 
=> "select * from `categories` where `categories`.`deleted_at` is null order by `created_at` desc" 

>>> App\Category::latest()->toSql() 
=> "select * from `categories` where `categories`.`deleted_at` is null order by `created_at` desc" 
+0

這顯示同樣的結果 – Neo

+0

你可以驗證,如果你的職位表中有created_at和的updated_at正確填充字段? –

+0

我已經使用種子創建了它們,但是手動更改了created_at datetime,也與另一個表具有相同的問題,以及我沒有使用種子將數據輸入表 – Neo

相關問題