![](/img/trans.png)
[英]laravel get all models with relations and the relations of these relations
[英]Get all relations name in laravel5 model
我有這樣的幾個關系模型,例如:
public function NewsCategory()
{
return $this->belongsTo("News\Model\NewsCategory");
}
public function NewsImage()
{
return $this->belongsTo("News\Model\NewsImage");
}
public function NewsTag()
{
return $this->belongsTo("News\Model\NewsTag");
}
關系會動態創建。
如何獲得所有此類名稱? 在這個例子中,我要
NewsCategory,NewsImage,NewsTag
$model_specific_method_name_array = array_diff(get_class_methods(<YourMOdel>), get_class_methods(<AnotherDummyEloquentModelWithoutAnyMethods>));
然后從數組中刪除模型上的其他已知方法。
一種方法如下:
$results = ModelClass::where(x,y)->with(['NewsCategory','NewsImage','NewsTag'])->first();
然后可以使用getRelations();
$relationshipKeys = array_keys($results->getRelations());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.