php 获取 Eloquent 模型的关系数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21615656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get array of Eloquent model's relations
提问by tprsn
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 thisdefinition 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 属性。但是,该方法返回一个空数组,并且关系属性是一个空数组,尽管我的关系设置正确。
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->relations 用于什么?有什么方法可以自动获取我的模型关系数组?
回答by The Alpha
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 Authormodel 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:
另外,正如我所说with,当你使用这样的东西时:
$article = Article::with('author')->get(1);
In this case, the first article (with id 1) will be loaded with it's related model Authorand you can use
在这种情况下,第一篇文章(id 1)将加载它的相关模型Author,您可以使用
$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 arrayyou may use toArray()method like:
要将它们转换为array您可以使用的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.
它在转换为数组后合并模型属性及其相关模型的属性,然后返回它。
回答by Johnson Oluwadele
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();
}

