⌘K
Noting Found

View all related articles

Laravel Strict Mode, All What You Need To Know.

Laravel 2 mins read

Table of Content

    Introduction

    Laravel is a powerful PHP framework, and with all that power, came big responsibilities. In today's article, we'll learn how to activate Eloquent Strict Mode in your Laravel application, and how to use them to your advantage.

    Requisites

    • MVC PHP.
    • Basics of the Laravel framework.
    • Laravel V9.35 and above.

    Prevent Lazy Loading

    Well, who may not know about Lazy Loading, is simply getting the parent model and it's children.

    In another story, let's say you have Post model, the Post has many comments, something like:

    1class Post extends Model
    2{
    3 // ...
    4 
    5 public function comments()
    6 {
    7 return $this->hasMany(Comment::class);
    8 }
    9}

    In order to retrieve, the comments for specific model, you will need to access the comments relationship

    1$post = Post::find(1);
    2$post->comments // return the comments collection.

    You may loop through the comments

    1@foreach($post->comments as $comment)
    2 {{ $comment->content }}
    3@endforeach

    But, let stop here for a moment. If you took a look at the query behind the scene, you will notice the N+1 Problem

    1SELECT * FROM `comments` WHERE `comments`. `id` = 1;
    2SELECT * FROM `comments` WHERE `comments`. `id` = 2;
    3SELECT * FROM `comments` WHERE `comments`. `id` = 3;
    4SELECT * FROM `comments` WHERE `comments`. `id` = 4;
    5...

    Let's say you have 1000 comments, this query, will repeat 1000 times, So in order to prevent that from happening, You'll use Lazy Loading Feature

    1$post = Post::find(1);
    2 
    3$post->with('comments')->comments;

    The with() method, will use in built-in function in MySQL to get all the comments, in a single query.

    1SELECT * FROM `comments` WHERE `comments` . `id` in (1,2,3,4,5,6,...1000)

    Because We're developers, and we forget most of the time :) We need to do something to remind us to do such thing, and prevent the disaster from happening, likely Laravel came with the feature to prevent it from the beginning.

    In boot method of your AppServiceProvider.php, add the following lines:

    1use Illuminate\Database\Eloquent\Model;
    2 
    3public function boot()
    4{
    5 Model::preventLazyLoading(! $this->app->isProduction());
    6}

    The ! $this->app->isProduction() simply telling the framework, if the app is in the production, don't bother to throw an error, and remind us to do the thing, because we're already messed up :nerd_face:

    Prevent Silently Discarding Attributes

    Another Strict Mode feature came with Laravel, is Preventing Silently Discarding Attributes, I know... Let's break this through.

    When you are inserting a new record, into the database through a model, you simply add the attributes, or we can say the columns of the table into the fillable array, something like this:

    1class User extends Model
    2{
    3 protected $fillable = [
    4 'name',
    5 'email',
    6 ];
    7}

    Let's say you added another column, gender for example, and you forget to add it in the fillable property, Laravel silently ignore that the new attribute is not in the fillable attributes, and just add it.

    To prevent that, and make sure that all the attributes are in the fillable array property, while developing your application, you may go into the boot method in AppServiceProvider.php as usual, and add the following:

    1public function boot()
    2{
    3 Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
    4}

    Prevent Access Missing Attributes

    You may notice while developing a Laravel application, and when accessing a missing attribute, Laravel will return NULL as a result.

    Let's say, you have a User Model, contains name, email and a password, and want to access something like gender, but it does not exist in the database, as a property

    1$user = User::find(1);
    2 
    3$user->gender // will returns null by default

    To prevent that from happening, you may add the following in the boot method

    1Model::preventAccessingMissingAttributes(! $this->app->isProduction());

    All In One

    If you liked what you saw above, and want to enable all 3 features at once, you may add the following line:

    1Model::shouldBeStrict(! $this->app->isProduction());

    Conclusion

    We discussed in this article, the 3 types of Model Strict Mode in Laravel, and how to implement it in your code base, and if you want to enable all 3 at once, you may use shouldBeStrict method.

    If you liked this type of articles, and you are interested in the web development in general, follow me on Twitter, for more tips and tricks on Laravel, and web development in general.

    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer | Technical Writer

    Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

    Comments

    Join our newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    We care about the protection of your data. Read our Privacy Policy.