How to check if a relation was loaded on an Eloquent model already?

October 26, 2018

Checking if a relation was loaded (maybe by a with() function call when using the Eloquent query builder) is easy.

From inside your model

If you want to do this within your model, you can use $this->relationLoaded():

<?php
// from inside your model:
if ($this->relationLoaded("comments")) {
	//..
}
From outside of your model
<?php
$user = User::first();
//
if ($user->relationLoaded("comments")) {
  //
}