2016-06-18 45 views
1

我有模型获取所有关系的名称包括,例如,几个关系是这样的:在laravel5模型

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

+0

选中此[方法](https://github.com/barryvdh/laravel-ide-helper/blob/master/src/Console/ModelsCommand.php#L306)。 – Burak

+0

我该如何使用它? – paranoid

+0

你可以使用这个模型:with('NewsCategory','NewsImage','NewsTag') - > get(); –

回答

2

一种方法是如下:

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

那么你可以使用getRelations();

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

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

然后取出从阵列模型其它已知的方法。