2016-06-18 43 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>));

然後取出從陣列模型其它已知的方法。