Laravel订购一个型号属于 [英] Laravel ordering a model belongsTo

查看:74
本文介绍了Laravel订购一个型号属于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型结构:

  class Family extends Model 
{
public function members( )
{
return $ this-> hasMany('App \Person');
}
}

class Person extends Model
{
public function family()
{
return $ this-> ;属于关联( 'App\Family');
}

public function school()
{
return $ this-> belongsTo('App \School');
}
}

class学校扩展模型
{
public function students()
{
return $ this-> ;的hasMany( 'App\Person');
}
}

简而言之,一个 属于许多学校,但只有一个 Family



虚拟数据:

  families_table 

id |名字
---------
1 |史密斯
2 | Doe

people_table

id | family_id | school_id |名称
----------------------------------
1 | 1 | 1 |贝蒂
2 | 2 | 1 | John

school_table

id |名字
-------------
1 | MIT

所以我们必须给用户:史密斯,贝蒂 Doe,John 。现在,如果我这样做:

  $ school = School :: find(1); 

foreach($ school-> student AS $ student){
echo $ student-> family-> name。 '''。 $ student-> name
}

我会得到:

 史密斯,贝蒂
Doe,John

我想看到的是:

  Doe,John 
史密斯,贝蒂

如何获取学校并列出成员根据 Family 表格中的名称字段排序

解决方案

最简单的方法是排序收集:

  $学校 - >学生 - >负载( '家人'); 
$ school-> students-> sortBy(function($ item){
return $ item-> family-> name;
});

您还可以使用连接对结果进行排序,如:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ - *')
- > join('families','families.id','=','students.family_id')
- > orderBy('families.name')
- > get();
$ school-> setRelation('学生',$学生);


This is my models structure:

class Family extends Model
{
    public function members()
    {
        return $this->hasMany('App\Person');
    }   
}

class Person extends Model
{
    public function family()
    {
        return $this->belongsTo('App\Family');
    }

    public function school()
    {
        return $this->belongsTo('App\School');
    }
}

class School extends Model
{
    public function students()
    {
        return $this->hasMany('App\Person');
    }
}

In short, a Person belongs to many Schools, but only one Family.

Dummy data:

families_table

id | name
---------
1  | Smith
2  | Doe

people_table

id | family_id | school_id | name
----------------------------------
1  | 1         | 1         | Betty
2  | 2         | 1         | John

school_table

id | name
-------------
1  | MIT

So we have to users: Smith, Betty and Doe, John. Now, if I do this:

$school = School::find(1);

foreach ($school->students AS $student) {
    echo $student->family->name . ', ' . $student->name
}

I will get:

Smith, Betty
Doe, John

What I want to see is:

Doe, John
Smith, Betty

How do I get the School and list the members sorted by the name field in from the Family table?

解决方案

The easiest way is by sorting collection:

$school->students->load('family');
$school->students->sortBy(function ($item) {
    return $item->family->name;
});

You can also sort results in query using join like:

$students = $school->students()
    ->with('family')
    ->select('students.*')
    ->join('families', 'families.id' , '=', 'students.family_id')
    ->orderBy('families.name')
    ->get();
$school->setRelation('students', $students);

这篇关于Laravel订购一个型号属于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆