Skip to content

Commit 188c994

Browse files
committed
Add global callback handler
Adds a way to call `Model::handleLazyLoadingViolationUsing()` from a service provider to add a way to handle violations for all models instead of overriding `violatedLazyLoading` on the models themselves.
1 parent 10f4f1c commit 188c994

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ public function isRelation($key)
468468
*/
469469
protected function violatedLazyLoading($key)
470470
{
471+
if (isset(static::$violatedLazyLoadingCallback)) {
472+
call_user_func(static::$violatedLazyLoadingCallback, $this, $key);
473+
return;
474+
}
475+
471476
throw new LazyLoadingViolationException($this, $key);
472477
}
473478

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializab
158158
*/
159159
protected static $modelsShouldPreventLazyLoading = false;
160160

161+
/**
162+
* The callback that is responsible for handing lazy loading violations.
163+
*
164+
* @var callable|null
165+
*/
166+
protected static $violatedLazyLoadingCallback;
167+
161168
/**
162169
* The name of the "created at" column.
163170
*
@@ -358,6 +365,16 @@ public static function preventLazyLoading($value = true)
358365
static::$modelsShouldPreventLazyLoading = $value;
359366
}
360367

368+
/**
369+
* Register a callback that is responsible for handling lazy loading violations.
370+
*
371+
* @param callable $callback
372+
*/
373+
public static function handleLazyLoadingViolationUsing(callable $callback)
374+
{
375+
static::$violatedLazyLoadingCallback = $callback;
376+
}
377+
361378
/**
362379
* Fill the model with an array of attributes.
363380
*

tests/Integration/Database/EloquentStrictLoadingTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ public function testStrictModeThrowsAnExceptionOnLazyLoadingInRelations()
100100

101101
$models[0]->modelTwos[0]->modelThrees;
102102
}
103+
104+
public function testStrictModeWithCustomCallbackOnLazyLoading()
105+
{
106+
$this->expectsEvents(ViolatedLazyLoadingEvent::class);
107+
108+
Model::handleLazyLoadingViolationUsing(function ($model, $key) {
109+
event(new ViolatedLazyLoadingEvent($model, $key));
110+
});
111+
112+
EloquentStrictLoadingTestModel1::create();
113+
EloquentStrictLoadingTestModel1::create();
114+
115+
$models = EloquentStrictLoadingTestModel1::get();
116+
117+
$models[0]->modelTwos;
118+
}
119+
120+
public function testStrictModeWithOverriddenHandlerOnLazyLoading()
121+
{
122+
$this->expectException(\RuntimeException::class);
123+
$this->expectExceptionMessage('Violated');
124+
125+
EloquentStrictLoadingTestModel1WithCustomHandler::create();
126+
EloquentStrictLoadingTestModel1WithCustomHandler::create();
127+
128+
$models = EloquentStrictLoadingTestModel1WithCustomHandler::get();
129+
130+
$models[0]->modelTwos;
131+
}
103132
}
104133

105134
class EloquentStrictLoadingTestModel1 extends Model
@@ -114,6 +143,23 @@ public function modelTwos()
114143
}
115144
}
116145

146+
class EloquentStrictLoadingTestModel1WithCustomHandler extends Model
147+
{
148+
public $table = 'test_model1';
149+
public $timestamps = false;
150+
protected $guarded = [];
151+
152+
public function modelTwos()
153+
{
154+
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
155+
}
156+
157+
protected function violatedLazyLoading($key)
158+
{
159+
throw new \RuntimeException("Violated {$key}");
160+
}
161+
}
162+
117163
class EloquentStrictLoadingTestModel2 extends Model
118164
{
119165
public $table = 'test_model2';
@@ -132,3 +178,15 @@ class EloquentStrictLoadingTestModel3 extends Model
132178
public $timestamps = false;
133179
protected $guarded = [];
134180
}
181+
182+
class ViolatedLazyLoadingEvent
183+
{
184+
public $model;
185+
public $key;
186+
187+
public function __construct($model, $key)
188+
{
189+
$this->model = $model;
190+
$this->key = $key;
191+
}
192+
}

0 commit comments

Comments
 (0)