May 2 · 2 min read

Optimization in Laravel

Eloquent is a powerful tool (active record pattern, if you wanna say in the technical term) in the Laravel framework which eases the application development by providing lot of things out of box. Few simpler things which could help in optimization using Eloquent:

  1. Use eager loading and avoid lazy loading:
    This is quite an important one while designing an application. Eager-loading removes multiple duplicate queries by fetching entities with a single one.
    `User:with(‘team’, ’teamDetails’)->get(); => preferred`
    User::get()->load(‘team’); => avoid, unless you really need
  2. Always fetch what you need:
    It’s quite common with developers to ignore fetching specific columns what you need in your application. Most of the time, developers tend to do:
    User::all(); or User::with(‘team’)->get(); => This will fetch all columns by default, if visibility restrictions have not been applied (i will talk about visibility later);
    You could do something like this:
    User::where(‘role’, ’vip’)->get([‘id’, ‘name’]); => In this way, only two columns will be fetched instead of all.
  3. Specify specific columns while loading relations:
    Do you know? you could also specify specific columns while loading relations instead of fetching all.
    User::with(‘team’)->get(); => this will fetch all columns of the user and it’s a respective team.
    But: User:with(‘team:id,name’)->get([‘id’,’name’,’team_id’); => this will fetch user with its columns id, name,team_id with relation team along with columns id and name.
    **Important** You must include foreign_key while specifying columns and also loading relations.
  4. Control columns serialization via Model:
    You could also control the columns being serialized by two following ways:
    a. declare columns in the visible property:
    e.g. protected $visible = [‘id’,’name’,’team_id’]; => In this way, whenever this model is serialized, only these columns will be exposed.
    b. declare columns in hidden property:
    e.g. protected $hidden = [‘sensitive_info’,’credit_card_details’]; => this will always make sure these columns are never exposed while serialization.
  5. Improve eloquent queries using scope:
    The scope is one of the cleanest ways to write a clean query using eloquent. Local and global scopes are two different ways to use.
    e.g. If you have something like this in your controller/somewhere else:
    User::where(‘role’, ‘vip’)->where(‘active’,true)->where(‘something’, ‘else’)…->get();
    Improvement: In User model: define a scope: e.g. public function scopeActiveVip($query){return $query->where(‘role’, ‘vip’)->where(‘active’,true)->where(‘something’, ‘else’);}
    Now, anywhere in your application: you could use :
    ** Much cleaner**
  6. Fancy things:
    There are many magical things: e.g.
    a. if you have User::where(‘role’, ‘vip’) => you could also write User::whereRole(‘vip’);
    b. if you wanna compare the date of a column, you do something like: User::whereDate(‘created_at’, now());

** Happy coding **