问题
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 wantNewsCategory,NewsImage,NewsTag
回答1:
one approach is to as follows :
$results = ModelClass::where(x,y)->with(['NewsCategory','NewsImage','NewsTag'])->first();
then you can use getRelations();
$relationshipKeys = array_keys($results->getRelations());
回答2:
$model_specific_method_name_array = array_diff(get_class_methods(<YourMOdel>), get_class_methods(<AnotherDummyEloquentModelWithoutAnyMethods>));
Then remove other known methods on your model from array.
来源:https://stackoverflow.com/questions/37896955/get-all-relations-name-in-laravel5-model