Laravel 5.1 方法 'orderBy' 未找到

Laravel 5.1 method 'orderBy' not found

Laravel 5.1 中的排序有问题。我是 Laravel 的新手,我的 php 技能不是很好。

当我使用查询生成器进行排序时,它的工作方式如下

$posts = DB::table('posts')
->where('post_status', 'draft')
->where('post_type', 'post')
->orderBy('post_date', 'asc')
->get();

当我尝试使用 Eloquent 时,它不会 像这样

$posts = Post::query()
->where('post_status', 'draft')
->where('post_type', 'post')
->orderBy('post_date', 'asc')
->get();

有人知道我做错了什么吗?

编辑: 我发现了问题。我正在使用 jgrossi/corcel 并且它已经在其生成器中进行了排序。所以我不能从控制器做到这一点。

删除 query() 表达式:

 $posts = Post::where('post_status', 'draft')
 ->where('post_type', 'post')
 ->orderBy('post_date', 'asc')
 ->get();

希望对您有所帮助。

您是否为您的模型添加了名称空间?之前的答案应该没问题

你加了吗

use App\Post;

也许粘贴异常是个好主意 .. 我知道 Laravel 的一件事......如果它在 tinker 中工作并且在视图中不起作用错误是在控制器或视图中......所以检查 tinker

我发现了问题。我正在使用 jgrossi/corcel 并且它已经在其生成器中进行了排序。所以我无法通过控制器进行操作。

这里是 Corcel 创作者:-)

将 Corcel 与 Laravel 5+ 一起使用,您必须包含像 Corcel\Post 这样的命名空间来引用 Post class。如果您想设置自己的连接名称:

class Post extends Corcel\Post {
    protected $connection = 'connection-name';
}

是的! Post 生成器已经订购了帖子:-)

有问题尽管问!向您致以最诚挚的问候并感谢您使用 Corcel。