2015-04-07 23 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

相关问题