简体   繁体   English

获取laravel5模型中的所有关系名称

[英]Get all relations name in laravel5 model

I have model include of several relation for example like this: 我有这样的几个关系模型,例如:

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");
}

and relations create dynamically. 关系会动态创建。
How can I get all this class name? 如何获得所有此类名称? In this example I want 在这个例子中,我要
NewsCategory,NewsImage,NewsTag

$model_specific_method_name_array = array_diff(get_class_methods(<YourMOdel>), get_class_methods(<AnotherDummyEloquentModelWithoutAnyMethods>));

然后从数组中删除模型上的其他已知方法。

one approach is to as follows : 一种方法如下:

$results = ModelClass::where(x,y)->with(['NewsCategory','NewsImage','NewsTag'])->first();

then you can use getRelations(); 然后可以使用getRelations();

$relationshipKeys = array_keys($results->getRelations());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM