You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-14Lines changed: 43 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,12 @@
1
1
# liam-wiltshire/laravel-jit-loader
2
2
3
-
liam-wiltshire/laravel-jit-loader is an extension to the default Laravel Eloquent model to 'very lazy eager load' relationships.
3
+
liam-wiltshire/laravel-jit-loader is an extension to the default Laravel Eloquent model to 'very lazy eager load' relationships with performance comparable with eager loading.
4
+
5
+
# Installation
6
+
liam-wiltshire/laravel-jit-loader is available as a composer package:
Once installed, use the `\LiamWiltshire\LaravelJitLoader\Concerns\AutoloadsRelationships` trait in your model, or have your models extend the `\LiamWiltshire\LaravelJitLoader\Model` class instead of the default eloquent model, and JIT loading will be automatically enabled.
4
10
5
11
# Very Lazy Eager Load?
6
12
In order to avoid [N+1 issues](https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/), you'd normally load your required relationships while building your collection:
@@ -23,7 +29,7 @@ In some situations however, this may not be possible - perhaps front-end develop
23
29
This change will track if your models belong to a collection, and if they do and a relationship is called that hasn't already been loaded, the relationship will be loaded across the whole collection just in time for use.
24
30
25
31
# Does This Work?
26
-
Yes. At least, it does in our production Laravel app. It's also been tested against a (rather constructed) test, pulling out staff, companies and addresses - while this isn't a 'real life' representation, it should give an idea of what it can do:
32
+
This is used in a number of production applications with no issues. It's also been tested against a (rather constructed) test, pulling out staff, companies and addresses - while this isn't a 'real life' representation, it should give an idea of what it can do:
27
33
28
34
```php
29
35
public function handle()
@@ -66,22 +72,45 @@ Yes. At least, it does in our production Laravel app. It's also been tested agai
66
72
67
73
Running this locally against a database with 200 companies, 1157 addresses and 39685 staff:
68
74
69
-
## Without JIT loading:
70
-
Queries Run: 10739
71
-
Execution Time: 16.058979034424
72
-
Memory:68MiB
75
+
## Without JIT Loading:
76
+
Queries Run: 10739<br />
77
+
Execution Time: 17.090859889984<br />
78
+
Memory: 70MiB
73
79
74
80
75
-
## With JIT loading:
76
-
Queries Run: 6
77
-
Execution Time: 1.6715261936188
78
-
Memory:26MiB
81
+
## With JIT Loading:
82
+
Queries Run: 3<br />
83
+
Execution Time: 1.7261669635773<br />
84
+
Memory:26MiB
79
85
80
-
# Installation
81
-
liam-wiltshire/laravel-jit-loader is available as a composer package:
Once installed, use the `\LiamWiltshire\LaravelJitLoader\Concerns\AutoloadsRelationships` trait in your model, or have your models extend the `\LiamWiltshire\LaravelJitLoader\Model` class instead of the default eloquent model, and JIT loading will be automatically enabled.
87
+
## 'Proper' Eager Loading:
88
+
Queries Run: 3<br />
89
+
Execution Time: 1.659285068512<br />
90
+
Memory: 26MiB
91
+
92
+
# Logging
93
+
As you can see the different between JIT loading and traditional eager loading is small (c. 0.067 seconds in our above test), so you can likely rely on JIT loader to protect you.
94
+
95
+
However, if you want to log when the JIT loader is used so that you can do back and correct them later, you can add a `$logChannel` property to your models to ask the trait to log into that channel as configured in Laravel
96
+
97
+
```php
98
+
class Address extends Model
99
+
{
100
+
use AutoloadsRelationships;
101
+
public $timestamps = false;
102
+
103
+
/**
104
+
* @var string
105
+
*/
106
+
protected $logChannel = 'jit-logger';
107
+
108
+
public function company()
109
+
{
110
+
return $this->belongsTo(Company::class);
111
+
}
112
+
}
113
+
```
85
114
86
115
# Limitations
87
116
This is an early release based on specific use cases. At the moment the autoloading will only be used when the relationship is loaded like a property e.g. `$user->company->name` instead of `$user->company()->first()->name`. I am working on supporting relations loaded in alternate ways, however there is more complexity in that so there isn't a fixed timescale as of yet!
0 commit comments