Skip to content

Instantly share code, notes, and snippets.

@reinink
Created February 5, 2021 15:18
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 reinink/af2154935f3ce99c53c8e7cbe450dcb6 to your computer and use it in GitHub Desktop.
Save reinink/af2154935f3ce99c53c8e7cbe450dcb6 to your computer and use it in GitHub Desktop.
Prevent relationship lazy-loading in Laravel
<?php
namespace App;
use Exception;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Model extends Eloquent
{
public function getRelationshipFromMethod($name)
{
$class = get_class($this);
throw new Exception("Lazy-loading relationships is not allowed ($class::$name).");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment