Laravel: Tips & Tricks for Your Organisation
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.
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.
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.
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:
- Base Class
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:
- Datatable – all my models will have datatable related functions.
- Audit Trail – all my models will have audit trail.
- 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.
There is tons of packages available out there for you to use. As for me, I’m stick with the following:
- Spatie: Laravel Permission – use for Access Control / RBAC
- Spatie: Media Library – use for Files Management
- Uxweb: Sweet Alert – Beautiful, wonderful, clean alert & prompt for your frontend.
- Yajra: Datatable
- OwenIT: Laravel Auditing – use for Audit Trail
- 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.
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:
- User Management
- Access Control / RBAC
- Audit Trail
- Capability to Handle File Uploads
- Standard UI / UX
- 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!
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.