Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created August 16, 2017 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cviebrock/dec80dedba34d112f9d537f5543c2cfc to your computer and use it in GitHub Desktop.
Save cviebrock/dec80dedba34d112f9d537f5543c2cfc to your computer and use it in GitHub Desktop.
Prevent lazy-loading Eloquent relations
<?php
/**
* Apply this trait to your models, and lazy-loading relations will throw an exception.
*/
trait DisableLazyLoading
{
protected function getRelationshipFromMethod($method)
{
$modelName = static::class;
throw new \LogicException(
"Attempting to lazy-load relation '$method' on model '$modelName'"
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment