Laravel: Tips & Tricks for Your Organisation

By | November 7, 2019

Assalamualaikum / Hi,

In an organisation, the development team usually have numbers of developers working on certain number of projects, and each projects has its own standards, common practices to deliver the projects.

Today, I’m going to share, what’s the tips & tricks from Laravel ecosystem that you can make use in your organisation, to speed up, standardise, and improve your team productivity.

Commands

Many of us, make use of artisan commands. But do you know, you can create your own commands, or even call multiple artisan commands at once?

Here are an example, a reload:cache command, which to clear all caches in an application:

Once above code created, you can run php artisan reload:cache.

Save minutes if you are about to write all those commands one by one.

You can see more sample codes I write in my Arch commands.

Routes

If you are working on medium to large applications, most likely you will hit this issue – messy routes in single file.

I came across one of the Spatie package, which simplify how I can manage my routes in better way. Look at the following codes:

It’s easy to understand right? Basically to get all PHP files in routes/web/ directory and include all the files in the directory.

Which means you can simply create as many route files in routes/web/ directory, the route will be loaded handsomely.

Your routes are much organised and save minutes, or even hours organising routes.

Here the implementation in my Arch Route Service Provider.

Model

In your model, usually you have numbers of functions, scopes and some other things that you think needed by the model.

Have you ever think that some of your functions, codes can be reuse? Yes, you can.

You have few options to reuse your codes:

  1. Traits
  2. Base Class
  3. Helpers

But right now i’m going to show the #2, Base Class. You can have something like the following:

As you can see above, number of traits I have setup in the Base model, such as:

  1. Datatable – all my models will have datatable related functions.
  2. Audit Trail – all my models will have audit trail.
  3. Media – all my models can have media, files.

So above are some of the traits I used in the Base model. Each time I create a new model, I just replace the default eloquent model class with my own Base model class.

Save minutes having this up in your application.

Here the implementation of my Base model in Arch.

Packages

There is tons of packages available out there for you to use. As for me, I’m stick with the following:

  1. Spatie: Laravel Permission – use for Access Control / RBAC
  2. Spatie: Media Library – use for Files Management
  3. Uxweb: Sweet Alert – Beautiful, wonderful, clean alert & prompt for your frontend.
  4. Yajra: Datatable
  5. OwenIT: Laravel Auditing – use for Audit Trail
  6. lab404: laravel-impersonate – I only enable the impersonate on non-production.

There is more packages I used as listed in my composer.json in Arch.

Boilerplate

What is boilerplate? Boilerplate it’s a base code for every new project which consist of common configurations, code practices, standards.

For most of my projects, personally I have Arch.

What’s for your organisation then? It’s up to you, but most of it are:

  1. User Management
  2. Access Control / RBAC
  3. Audit Trail
  4. Capability to Handle File Uploads
  5. Standard UI / UX
  6. SOP on development

If I want to list, it will be a lot. But I bet you get the idea – keep it DRY at your application base code level.

Boilerplate – save hours, days or even months for your organisation! Time is gold!

Finale

Finally, hoping the tips and tricks will help your organisation to speed up team development, standardised practices and build your own boilerplate for your organisation or even for your personal use.

My slide on this can be found at: bit.ly/config-2019.

Leave a Reply

Your email address will not be published. Required fields are marked *

15 + one =