File tree Expand file tree Collapse file tree 4 files changed +46
-2
lines changed
src/Illuminate/Database/Eloquent Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -520,6 +520,10 @@ protected function handleLazyLoadingViolation($key)
520
520
return call_user_func (static ::$ lazyLoadingViolationCallback , $ this , $ key );
521
521
}
522
522
523
+ if (! $ this ->exists || $ this ->wasRecentlyCreated ) {
524
+ return ;
525
+ }
526
+
523
527
throw new LazyLoadingViolationException ($ this , $ key );
524
528
}
525
529
Original file line number Diff line number Diff line change @@ -384,10 +384,10 @@ public static function preventLazyLoading($value = true)
384
384
/**
385
385
* Register a callback that is responsible for handling lazy loading violations.
386
386
*
387
- * @param callable $callback
387
+ * @param callable|null $callback
388
388
* @return void
389
389
*/
390
- public static function handleLazyLoadingViolationUsing (callable $ callback )
390
+ public static function handleLazyLoadingViolationUsing (? callable $ callback )
391
391
{
392
392
static ::$ lazyLoadingViolationCallback = $ callback ;
393
393
}
Original file line number Diff line number Diff line change @@ -160,6 +160,18 @@ public function testExceptionThrownWhenPayloadIsInvalid()
160
160
$ e ->decrypt ($ payload );
161
161
}
162
162
163
+ public function testDecryptionExceptionIsThrownWhenUnexpectedTagIsAdded ()
164
+ {
165
+ $ this ->expectException (DecryptException::class);
166
+ $ this ->expectExceptionMessage ('Unable to use tag because the cipher algorithm does not support AEAD. ' );
167
+
168
+ $ e = new Encrypter (str_repeat ('a ' , 16 ));
169
+ $ payload = $ e ->encrypt ('foo ' );
170
+ $ decodedPayload = json_decode (base64_decode ($ payload ));
171
+ $ decodedPayload ->tag = 'set-manually ' ;
172
+ $ e ->decrypt (base64_encode (json_encode ($ decodedPayload )));
173
+ }
174
+
163
175
public function testExceptionThrownWithDifferentKey ()
164
176
{
165
177
$ this ->expectException (DecryptException::class);
Original file line number Diff line number Diff line change @@ -145,6 +145,21 @@ public function testStrictModeWithOverriddenHandlerOnLazyLoading()
145
145
146
146
$ models [0 ]->modelTwos ;
147
147
}
148
+
149
+ public function testStrictModeDoesntThrowAnExceptionOnManuallyMadeModel ()
150
+ {
151
+ $ model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::make ();
152
+ $ model2 = EloquentStrictLoadingTestModel2::make ();
153
+ $ model1 ->modelTwos ->push ($ model2 );
154
+
155
+ $ this ->assertInstanceOf (Collection::class, $ model1 ->modelTwos );
156
+ }
157
+
158
+ public function testStrictModeDoesntThrowAnExceptionOnRecentlyCreatedModel ()
159
+ {
160
+ $ model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::create ();
161
+ $ this ->assertInstanceOf (Collection::class, $ model1 ->modelTwos );
162
+ }
148
163
}
149
164
150
165
class EloquentStrictLoadingTestModel1 extends Model
@@ -176,6 +191,19 @@ protected function handleLazyLoadingViolation($key)
176
191
}
177
192
}
178
193
194
+ class EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading extends Model
195
+ {
196
+ public $ table = 'test_model1 ' ;
197
+ public $ timestamps = false ;
198
+ public $ preventsLazyLoading = true ;
199
+ protected $ guarded = [];
200
+
201
+ public function modelTwos ()
202
+ {
203
+ return $ this ->hasMany (EloquentStrictLoadingTestModel2::class, 'model_1_id ' );
204
+ }
205
+ }
206
+
179
207
class EloquentStrictLoadingTestModel2 extends Model
180
208
{
181
209
public $ table = 'test_model2 ' ;
You can’t perform that action at this time.
0 commit comments