繁体   English   中英

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

[英]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.

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