π¨ Laravel developers, you may or may not have heard of the shouldBeStrict() method of Laravel's Model. Itβs a game-changer for enforcing strict rules that protect your app from unexpected bugs. π
Want to learn how it can level up your development? π€
Keep reading! π§΅
Want to learn how it can level up your development? π€
Keep reading! π§΅
Comments
1οΈβ£ Prevents lazy loading
2οΈβ£ Prevents silently discarding attributes
3οΈβ£ Prevents accessing missing attributes
Letβs break down these behaviors one by one π
π΄ Before enabling strictness:
Relationships are loaded lazily, silently triggering N+1 queries.
π’ After enabling preventLazyLoading():
Strict mode throws a LazyLoadingViolationException
π΄ Before enabling strictness:
Attributes not in fillable or guarded are silently discarded without any warning:
π’ After enabling preventSilentlyDiscardingAttributes():
Strict mode throws a MassAssignmentException when undefined attributes are saved.
π΄ Before enabling strictness:
Accessing a non-existent attribute simply returns null with no error:
π’ After enabling preventAccessingMissingAttributes():
Strict mode throws a MissingAttributeException when trying to access undefined attributes
This way, your development stays strict, but production is monitored without causing disruptions. π‘π
Enforce strict rules, handle violations, and keep your code clean. Happy coding! π¨βπ»π©βπ»
#Laravel #PHP