获取一系列Eloquent模型的关系

时间:2022-10-16 08:34:57

I'm trying to get an array of all of my model's associations. I have the following model:

我正在尝试获取所有模型的关联数组。我有以下型号:

class Article extends Eloquent 
{
    protected $guarded = array();

    public static $rules = array();

    public function author() 
    {
        return $this->belongsTo('Author');
    }

    public function category() 
    {
        return $this->belongsTo('Category');
    }
}

From this model, I'm trying to get the following array of its relations:

从这个模型,我试图获得以下关系数组:

array(
    'author',
    'category'
)

I'm looking for a way to pull this array out from the model automatically.

我正在寻找一种方法从模型中自动拉出这个数组。

I've found this definition of a relationsToArray method on an Eloquent model, which appears to return an array of the model's relations. It seems to use the $this->relations attribute of the Eloquent model. However, this method returns an empty array, and the relations attribute is an empty array, despite having my relations set up correctly.

我在Eloquent模型上找到了一个relationsToArray方法的定义,它似乎返回了模型关系的数组。它似乎使用了Eloquent模型的$ this-> relations属性。但是,此方法返回一个空数组,而relations属性是一个空数组,尽管我的关系设置正确。

What is $this->relations used for if not to store model relations? Is there any way that I can get an array of my model's relations automatically?

什么是$ this->如果不存储模型关系则使用关系?有什么方法可以自动获得模型关系的数组吗?

2 个解决方案

#1


21  

It's not possible because relationships are loaded only when requested either by using with (for eager loading) or using relationship public method defined in the model, for example, if a Author model is created with following relationship

这是不可能的,因为只有在使用with(用于预先加载)或使用模型中定义的关系公共方法(例如,如果创建具有以下关系的Author模型)请求时才加载关系

public function articles() {
    return $this->hasMany('Article');
}

When you call this method like:

当您调用此方法时:

$author = Author::find(1);
$author->articles; // <-- this will load related article models as a collection

Also, as I said with, when you use something like this:

另外,正如我所说,当你使用这样的东西时:

$article = Article::with('author')->get(1);

In this case, the first article (with id 1) will be loaded with it's related model Author and you can use

在这种情况下,第一篇文章(ID为1)将加载它的相关模型作者,您可以使用

$article->author->name; // to access the name field from related/loaded author model

So, it's not possible to get the relations magically without using appropriate method for loading of relationships but once you load the relationship (related models) then you may use something like this to get the relations:

因此,如果不使用适当的方法来加载关系,就不可能神奇地获得关系,但是一旦加载了关系(相关模型),那么你可以使用这样的东西来获得关系:

$article = Article::with(['category', 'author'])->first();
$article->getRelations(); // get all the related models
$article->getRelation('author'); // to get only related author model

To convert them to an array you may use toArray() method like:

要将它们转换为数组,您可以使用toArray()方法,如:

dd($article->getRelations()->toArray()); // dump and die as array

The relationsToArray() method works on a model which is loaded with it's related models. This method converts related models to array form where toArray() method converts all the data of a model (with relationship) to array, here is the source code:

relationsToArray()方法适用于加载了相关模型的模型。此方法将相关模型转换为数组形式,其中toArray()方法将模型的所有数据(带关系)转换为数组,这里是源代码:

public function toArray()
{
     $attributes = $this->attributesToArray();

     return array_merge($attributes, $this->relationsToArray());
}

It merges model attributes and it's related model's attributes after converting to array then returns it.

它在转换为数组之后合并模型属性及其相关模型的属性,然后返回它。

#2


3  

use this:

class Article extends Eloquent 
{
    protected $guarded = array();

    public static $rules = array();

    public $relationships = array('Author', 'Category');

    public function author() {
        return $this->belongsTo('Author');
    }

    public function category() {
        return $this->belongsTo('Category');
    }
}

So outside the class you can do something like this:

所以在课外你可以这样做:

public function articleWithAllRelationships()
{
    $article = new Article;
    $relationships = $article->relationships;
    $article = $article->with($relationships)->first();
}

#1


21  

It's not possible because relationships are loaded only when requested either by using with (for eager loading) or using relationship public method defined in the model, for example, if a Author model is created with following relationship

这是不可能的,因为只有在使用with(用于预先加载)或使用模型中定义的关系公共方法(例如,如果创建具有以下关系的Author模型)请求时才加载关系

public function articles() {
    return $this->hasMany('Article');
}

When you call this method like:

当您调用此方法时:

$author = Author::find(1);
$author->articles; // <-- this will load related article models as a collection

Also, as I said with, when you use something like this:

另外,正如我所说,当你使用这样的东西时:

$article = Article::with('author')->get(1);

In this case, the first article (with id 1) will be loaded with it's related model Author and you can use

在这种情况下,第一篇文章(ID为1)将加载它的相关模型作者,您可以使用

$article->author->name; // to access the name field from related/loaded author model

So, it's not possible to get the relations magically without using appropriate method for loading of relationships but once you load the relationship (related models) then you may use something like this to get the relations:

因此,如果不使用适当的方法来加载关系,就不可能神奇地获得关系,但是一旦加载了关系(相关模型),那么你可以使用这样的东西来获得关系:

$article = Article::with(['category', 'author'])->first();
$article->getRelations(); // get all the related models
$article->getRelation('author'); // to get only related author model

To convert them to an array you may use toArray() method like:

要将它们转换为数组,您可以使用toArray()方法,如:

dd($article->getRelations()->toArray()); // dump and die as array

The relationsToArray() method works on a model which is loaded with it's related models. This method converts related models to array form where toArray() method converts all the data of a model (with relationship) to array, here is the source code:

relationsToArray()方法适用于加载了相关模型的模型。此方法将相关模型转换为数组形式,其中toArray()方法将模型的所有数据(带关系)转换为数组,这里是源代码:

public function toArray()
{
     $attributes = $this->attributesToArray();

     return array_merge($attributes, $this->relationsToArray());
}

It merges model attributes and it's related model's attributes after converting to array then returns it.

它在转换为数组之后合并模型属性及其相关模型的属性,然后返回它。

#2


3  

use this:

class Article extends Eloquent 
{
    protected $guarded = array();

    public static $rules = array();

    public $relationships = array('Author', 'Category');

    public function author() {
        return $this->belongsTo('Author');
    }

    public function category() {
        return $this->belongsTo('Category');
    }
}

So outside the class you can do something like this:

所以在课外你可以这样做:

public function articleWithAllRelationships()
{
    $article = new Article;
    $relationships = $article->relationships;
    $article = $article->with($relationships)->first();
}