DEV Community

Yogesh Galav
Yogesh Galav

Posted on • Updated on

How to write clean code in Laravel?

Laravel is the most beautiful thing ever done to PHP. But most developers working in project based company like in India don't utilize it to make there strength. The best thing about it is predefined structure but of-course people break it bcoz any code written in core PHP works inside Laravel. I'm writing this blog to transfer my knowledge of writing clean code in Laravel hence making projects and products maintainable and beautiful.

I will explain this in following points along with MVC

1. Write Clean PHP
1.a There are many rules for writing standard PHP code, I cannot list them all here hence just use tools which can fix your code.
1.b Use PHP CodeSniffer, Use PHP-CS-Fixer, Use Laravel Pint or just use Duster package for Laravel.
1.c Duster is a tool that brings together Laravel Pint, PHP CodeSniffer, PHP-CS-Fixer, and Tighten's Laravel-specific lints in Tlint to provide a powerful and comprehensive linting and fixing toolset for Laravel apps.

2. Write Clean Models
2.a Models are the representation of database table in object format hence clean Model should only contain respective table information.
2.b Model should tell us which are all the field present in table without looking in database by mentioning them Fillables.
2.c Mention all the Relationships with other tables.
2.d Use Mutators & Casting for formatting fields before using them.
2.e Use Scopes for making eloquent queries short in respect to particular table.
2.f Don't write any additional code like db queries or heavy functions. It will eventually deteriorate the quality of model.

3. Write Clean Views
3.a Use single master template. you can put conditions in that if you want separate design for different user types.
3.b Don't call eloquent queries in views.
3.c Keep folder structure exactly like url in pages folder even if you use frontend framework like vue or react so you can also use folder based routing.
3.d Keep Header, Sidebar and footer like component inside "includes" folder.
3.e Keep layouts folder in case you have different user type layouts.
3.f Keep your JS at one place(file or folder) and css at one place.
3.g Use single blade file in case of Vue SPA or Inerita.

4. Write Clean Controllers
4.a Controller is the place where business logic resides and hence it's also the place where most of the mess take place
4.b Keep validation logic separately in FormRequest classes. They are also simple to use and understand.
4.c Keep function name simple as index, store, create, update, delete, download etc. Don't use random names like getUserData or saveUser.
4.d If a particular function like index fetches two or more items it's ideal to make child functions which you can declare in Helpers, Services, Actions, Jobs etc
In this case try to keep if-else and for-loop inside controller function only.
4.e If the function doesn't use much code then it's ideal to use Models or DB query directly.
4.f Don't use try catch everywhere. it's only useable in case of multiple insertion into database for rollback. Else let the error come in logs and fix it.
4.g Use Resource Classes for json output. Or atleast keep the format of response same everywhere.

I hope you enjoyed reading this.
Thankyou and have a nice day!

Top comments (0)