問題描述
我正在嘗試獲取所有模型關聯的數組.我有以下模型:
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 模型的關系數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!