Eloquent Log Lazy Loading
[!WARNING]
This package is abandonned as it hasn't been updated since Laravel 5,
and the functionality has been baked into recent versions of Laravel via the
Model::handleLazyLoadingViolationUsing method., (*1)
Log (or disable) Eloquent lazy-loaded relationships in Laravel 5 to speed up your application., (*2)
, (*3)
Why You Might Want This Package
Eloquent's lazy-loading is great. You can do Group::find($id) in your controller, and then loop through
$group->users in your template without needing to worry about the database queries that run under-the-hood., (*4)
The problem comes when you try and loop through $group->users->addresses, or some other relation. You may
inadvertently be running an SQL query for each iteration of that loop. If you are running a site that gets
millions of hits a day, this might not be a good thing!, (*5)
By logging where your script is lazy-loading relationships, you may be able to optimize your application to avoid
unnecessary database calls. At the very least you can see where the bottlenecks are. And, if you want, you can
disable lazy-loading entirely and only allow explicitly loaded relations from being accessed., (*6)
Installation
Install the package via composer:, (*7)
$ composer require cviebrock/eloquent-log-lazy-loading
That's it!, (*8)
Usage
Your models should use the LogLazyLoading trait:, (*9)
use Cviebrock\EloquentLogLazyLoading\LogLazyLoading;
class MyModel extends Eloquent
{
use LogLazyLoading;
}
When you attempt to lazy load a relationship, the package will report a LazyLoadingException exception and continue
normally (i.e. it will load the relationship). This is a great way to check where your application is doing
lazy-loading and allows you to refactor, possibly reducing the number of database queries that are called., (*10)
However, if you really want to go hard core and halt your application when a relation is lazy-loaded, then just
set the disableLazyLoading property on your model to true. The exception will be thrown, and not just reported., (*11)
use Cviebrock\EloquentLogLazyLoading\LogLazyLoading;
class MyModel extends Eloquent
{
use LogLazyLoading;
protected $disableLazyLoading = true;
}
Bugs, Suggestions and Contributions
Thanks to everyone
who has contributed to this project., (*12)
Please use Github for reporting bugs,
and making comments or suggestions., (*13)
See CONTRIBUTING.md for how to contribute changes., (*14)
Copyright and License
eloquent-taggable
was written by Colin Viebrock and is released under the
MIT License., (*15)
Copyright (c) 2017 Colin Viebrock, (*16)