获取 Eloquent 模型的关系数组

时间:2023-03-04
本文介绍了获取 Eloquent 模型的关系数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

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.

如果不存储模型关系,$this->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?

推荐答案

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

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

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

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

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

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

在这种情况下,第一篇文章(id为1)将加载其相关模型Author,您可以使用

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

$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

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

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

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

relationsToArray() 方法适用于加载了相关模型的模型.该方法将相关模型转换为数组形式,其中toArray()方法将模型(有关系)的所有数据转换为数组,源代码如下:

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:

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.

这篇关于获取 Eloquent 模型的关系数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Laravel Eloquent 和多重连接 下一篇:如何从原始对象创建 Eloquent 模型实例?

相关文章