简体   繁体   English

如何在 Laravel 中链接 Eloquent 方法?

[英]How do you chain Eloquent methods in Laravel?

Using Laravel and mySQL - I'm trying to query a collection to return an array of entries sorted by the column 'course_id' and only the entries that have the same 'user_id'.使用 Laravel 和 mySQL - 我正在尝试查询集合以返回按“course_id”列排序的条目数组,并且仅返回具有相同“user_id”的条目。 I keep getting a '500 internal server error'.我不断收到“500 内部服务器错误”。

I've tried different ways of doing it and also been reading the Eloquent docs but still stuck!我尝试了不同的方法,并且还阅读了 Eloquent 文档,但仍然卡住了!

Route::get('roadmap/{id}', function ($user_id) {
  return RoadmapCourse::where('user_id', $user_id)->sortBy('course_id'); 
});

It works fine if I take off the sortBy():如果我取消 sortBy(),它工作正常:

  return RoadmapCourse::where('user_id', $user_id)->get();

But i'm trying to sort it by 'course_id'.但我正在尝试按“course_id”对其进行排序。

Thanks!谢谢!

Try with orderBy() instead of sortBy() :尝试使用orderBy()而不是sortBy()

return RoadmapCourse::where('user_id', $user_id)->orderBy('course_id','asc')->get(); return RoadmapCourse::where('user_id', $user_id)->orderBy('course_id','asc')->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM