Laravel Eloquent:如何订购相关模型的结果?

时间:2022-10-15 23:16:40

I have a model called School and it has many Students .

我有一个名为School的模型,它有很多学生。

Here is the code in my model:

这是我模型中的代码:

public function students()
{
    return $this->hasMany('Student');
}

I am getting all the students with this code in my controller:

我正在让所有学生在我的控制器中使用此代码:

$school = School::find($schoolId);

and in the view:

并在视图中:

@foreach ($school->students as $student)

Now I want to order the Students by some field in the students table. How can I do that?

现在我想通过学生表中的某个字段订购学生。我怎样才能做到这一点?

3 个解决方案

#1


32  

You have a few ways of achieving this:

您有几种方法可以实现此目的:

// when eager loading
$school = School::with(['students' => function ($q) {
  $q->orderBy('whateverField', 'asc/desc');
}])->find($schoolId);

// when lazy loading
$school = School::find($schoolId);
$school->load(['students' => function ($q) {
  $q->orderBy('whateverField', 'asc/desc');
}]);

// or on the collection
$school = School::find($schoolId);
// asc
$school->students->sortBy('whateverProperty');
// desc
$school->students->sortByDesc('whateverProperty');


// or querying students directly
$students = Student::whereHas('school', function ($q) use ($schoolId) {
  $q->where('id', $schoolId);
})->orderBy('whateverField')->get();

#2


5  

To answer the original question, the students dynamic property can also be accessed as a relationship method.

要回答原始问题,学生动态属性也可以作为关系方法访问。

So you have this to fetch all students:

所以你有这个来取得所有学生:

$students = $school->students;

Now as a relationship method, this is equivalent:

现在作为一种关系方法,这是等价的:

$students = $school->students()->get();

Given this, you can now add in some ordering:

鉴于此,您现在可以添加一些顺序:

$students = $school->students()->orderBy('students.last_name')->get();

Since eloquent will be performing a join, make sure to include the table name when referencing the column to order by.

由于eloquent将执行连接,因此请确保在引用要排序的列时包含表名。

You can also add this to your students method if you want to set a default order that $school->students will always return. Check out the documentation for hasMany() to see how this works.

如果您想设置$ school->学生将始终返回的默认顺序,您也可以将其添加到学生方法中。查看hasMany()的文档,了解其工作原理。

#3


5  

you can add orderBy to your relation, so the only thing you need to change is

你可以为你的关系添加orderBy,所以你唯一需要改变的是

public function students()
{
    return $this->hasMany('Student');
}

to

public function students()
{
    return $this->hasMany('Student')->orderBy('id', 'desc')
}

#1


32  

You have a few ways of achieving this:

您有几种方法可以实现此目的:

// when eager loading
$school = School::with(['students' => function ($q) {
  $q->orderBy('whateverField', 'asc/desc');
}])->find($schoolId);

// when lazy loading
$school = School::find($schoolId);
$school->load(['students' => function ($q) {
  $q->orderBy('whateverField', 'asc/desc');
}]);

// or on the collection
$school = School::find($schoolId);
// asc
$school->students->sortBy('whateverProperty');
// desc
$school->students->sortByDesc('whateverProperty');


// or querying students directly
$students = Student::whereHas('school', function ($q) use ($schoolId) {
  $q->where('id', $schoolId);
})->orderBy('whateverField')->get();

#2


5  

To answer the original question, the students dynamic property can also be accessed as a relationship method.

要回答原始问题,学生动态属性也可以作为关系方法访问。

So you have this to fetch all students:

所以你有这个来取得所有学生:

$students = $school->students;

Now as a relationship method, this is equivalent:

现在作为一种关系方法,这是等价的:

$students = $school->students()->get();

Given this, you can now add in some ordering:

鉴于此,您现在可以添加一些顺序:

$students = $school->students()->orderBy('students.last_name')->get();

Since eloquent will be performing a join, make sure to include the table name when referencing the column to order by.

由于eloquent将执行连接,因此请确保在引用要排序的列时包含表名。

You can also add this to your students method if you want to set a default order that $school->students will always return. Check out the documentation for hasMany() to see how this works.

如果您想设置$ school->学生将始终返回的默认顺序,您也可以将其添加到学生方法中。查看hasMany()的文档,了解其工作原理。

#3


5  

you can add orderBy to your relation, so the only thing you need to change is

你可以为你的关系添加orderBy,所以你唯一需要改变的是

public function students()
{
    return $this->hasMany('Student');
}

to

public function students()
{
    return $this->hasMany('Student')->orderBy('id', 'desc')
}