No, no, at least not without extra work, because your model does not know what kind of relationship it maintains until it is actually loaded.
I had this problem in one of my own Laravel packages. There is no way to get a list of model relationships with Laravel. This is pretty obvious, though, if you look at how they are defined. Simple functions that return a Relation object. You can't even get the return type of a function with php reflection classes, so there is no way to distinguish a relation function from any other function.
To make things easier, you can define a function that adds all relationships. You can use the eloquents query areas for this (thanks to Jarek Tkaczyk for mentioning this in the comments).
public function scopeWithAll($query) { $query->with('foo', 'bar', 'baz'); }
Using scopes instead of static functions allows you to use not only your function directly in the model, but also, for example, when combining query builder methods, such as where , in any order:
Model::where('something', 'Lorem ipsum dolor')->withAll()->where('somethingelse', '>', 10)->get();
Alternatives for Getting A Relationship
Although Laravel does not support something like this out of the box, you can always add it yourself.
Annotations
I used annotations to determine if a function is a relation or not in my package mentioned above. Annotations are not officially part of php, but many people use doc blocks to model them. Laravel 5 is also going to use annotations in its route definitions, so I decided that this would not be bad practice in this case. The advantage is that you do not need to maintain a separate list of supported relationships.
Add annotation to each of your relationships:
public function foo() { return $this->belongsTo('Foo'); }
And write a function that parses document blocks of all methods in the model and returns a name. You can do this in the model or in the parent class:
public static function getSupportedRelations() { $relations = []; $reflextionClass = new ReflectionClass(get_called_class()); foreach($reflextionClass->getMethods() as $method) { $doc = $method->getDocComment(); if($doc && strpos($doc, '@Relation') !== false) { $relations[] = $method->getName(); } } return $relations; }
And then just use them in your withAll function:
public function scopeWithAll($query) { $query->with($this->getSupportedRelations()); }
Some like php annotations, and some don't. I like this simple use case.
Array of maintained relationships
You can also maintain an array of all supported relationships. However, this requires that you always synchronize it with the available relationships, which is not always so simple, especially if several developers participate in it.
protected $supportedRelations = ['foo','bar', 'baz'];
And then just use them in your withAll function:
public function scopeWithAll($query) { return $query->with($this->supportedRelations); }
Of course, you can also override with , like the Lucasgater mentioned in his answer . This seems cleaner than using withAll . If you use annotations or an array of configuration, however, this is a matter of opinion.