laravel 5:latest() - > get()没有给出准确的结果

时间:2015-04-07 18:32:30

标签: laravel

我是Laravel的新手,我正在开发一个关于Laravel 5的项目。

我试图根据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 {

    //

   }

请告诉我我做错了什么?

提前致谢

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:1)

你试过喜欢这个吗?:

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

}