获得一系列雄辩模特的关系 [英] Get array of Eloquent model's relations

查看:104
本文介绍了获得一系列雄辩模特的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

public static $ rules = array();

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

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

从这个模型,我试图得到其关系如下:

  array(
'author',
'category'

我正在寻找一种将该阵列自动从模型中拉出的方法。 p>

我发现这个在一个Eloquent模型上的relationshipToArray方法的定义,它似乎返回一个模型关系的数组。似乎使用Eloquent模型的$ this-> relationships属性。但是,该方法返回一个空数组,而且关系属性是一个空数组,尽管我的关系正确设置。



什么是$ this->关系用于如果不存储模型关系?有没有办法可以自动获取我的模型关系数组?

解决方案

这是不可能的,因为只有当通过使用(热切加载)或使用模型中定义的关系公共方法来请求,例如,如果作者模型创建具有以下关系

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

当您调用此方法时,如下所示:

  $ author =作者:: find(1); 
$ author->文章; //< - 这将加载相关的文章模型作为集合

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

  $ article = :用( '作者') - >获得(1); 

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

  $ article-> author-> name ; //从相关/加载的作者模型访问名称字段

所以,不可能得到关系神奇地没有使用适当的方式加载关系,但一旦你加载关系(相关模型),那么你可以使用这样的东西来获得关系:

  $ article = article :: with(['category','author']) - > first(); 
$ article-> getRelations(); //获取所有相关模型
$ article-> getRelation('author'); //只得到相关的作者模型

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

  DD($物品─> getRelations() - >指定者()); // dump and die as array 

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

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

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

它将模型属性和转换为数组后的相关模型的属性合并,然后返回。


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.

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?

解决方案

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

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

$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:

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:

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.

这篇关于获得一系列雄辩模特的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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