How Laravel Helps Me Code Less...

By Dibyendu Singha Roy | Nov 12, 2018

Wondering how to get more from your code? Is it possible to code less and do more? Our expert has the answers.

Once upon a time, a great man said “The smarter you get the less you speak.” If we relate the quote to the programmer's world, it would say the smarter we get the less we code.

I’m sure you are wondering how? So, today our topic is ‘Less Coding.’ During the very beginning of my life as a PHP programmer, I used to code a lot.

We will take a simple example of registering a user and storing the data in our database.

For this simple work of registering a user, this is what I used to do:

  • Create an HTML form
  • Give proper names to input elements
  • Do some client-side validation
  • Submit it with proper data

Then there comes the server-side part of it:

  1. We get the entire thing in $_POST array
  2. Check individually all the mandatory fields
  3. If any mandatory field is missing we set an error flag for that field
  4. After which, we check all other fields
  5. We check for the uniqueness of the user_name and that also involves some lines of code for database interaction
  6. We check the error flags, and if we find one we don’t proceed and send a message with all the error_flags to the HTML
  7. If all is fine then we write PHP MySQL interaction functions to insert data inside and inside that function we write INSERT INTO BLA BLABLAsql

These 7 Points will take at least 100 lines of code – from getting data to validation and finally Insertion.

Now we go back to point number 3 – For example, this may be a missing mandatory field: Validating a user mobile number

  • We have to first check if it is empty or not
  • it should not exceed 10 digits.
  • It should be numbers only

Let’s have a look at how we can achieve this in PHP

  • To check the mobile_number is empty or not, we can do this
    • if(empty($_POST[‘mobile_number’])){}
  • Now if somebody enters blank space then empty() won’t work in that case we need to use trim()
    • if(empty(trim($_POST[‘mobile_number’]))){}
  • Now we need to check whether it is a number or not and for that we can use is_number();
    • if(empty(trim($_POST[‘mobile_number’])) && !is_number($_POST[‘mobile_number’])){ }
  • Well… wait a minute we can use this is_number() to check all the problems because empty, null or blank spaces are not numbers hence we can get rid of empty(trim($_POST[‘mobile_number’])) part
    • if(!is_number($_POST[‘mobile_number’])){ }
  • Now it should be of 10 digits only and for that we need to use strlen() function
    • if(is_number($_POST[‘mobile_number’]) &&strlen($_POST[‘mobile_number’]) != 10 ){}
  • Now suddenly you started feeling that why should I make the phone number as a mandatory field ? But If somebody enters something as phone number then it should be Numeric and must have 10 digits and to achieve that we need to wrap the whole thing in another if() statement
    • if(isset($_POST[‘mobile_number’]) && !empty($_POST[‘mobile_number’])) { if(is_number($_POST[‘mobile_number’]) &&strlen($_POST[‘mobile_number’]) != 10 ){ } }

Now let’s see how we can achieve this entire logic using latest PHP framework Laravel.

Laravel is an MVC framework and to know what MVC is you can check this link.

To obtain an instance of the current HTTP request via dependency injection, we should type-hint the Illuminate\Http\Request class on your controller method

<?PHP namespace App\Http\Controllers;
use Illuminate\Http\Request;
classUserController extends Controller {} ?>

Now to do the validation, we will use the validate method provided by the Illuminate\Http\Request object.

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
classUserController extends Controller
{public function store(Request $request)
{$validatedData = $request->validate([…..‘mobile_number’ => ‘required|numeric|digits:10’]);}}?>

This will make the the mobile_number as mandatory field and check if it is numeric or not and also it must have 10 digits exactly.

Now if you want to make the mobile_number field optional just remove the required portion of the validation rule.

$validatedData = $request->validate([…..‘mobile_number’ => ‘numeric|digits:10’]);

Now suddenly you decide that multiple users cannot have the same mobile number so you have to make it unique.

This is a big challenge in custom PHP. You need to write the query to check whether any user exists with the same mobile_number then check the number of rows, then if the row count is 0 proceed else to stop.

But in laravel just do this.

$validatedData = $request->validate([…..‘mobile_number’ => ‘numeric|digits:10|unique:users’]);

This will automatically query through users table and check the uniqueness of the mobile_number

Isn’t this simple?

So from now onwards let’s focus on less coding because LESS IS MORE.

We can provide end-to-end digital activation, right from the creation of a web portal to a custom-built digital marketing strategy to help you reach the entire world.

Looking for a place to start?

user picture

Dibyendu Singha Roy

PHP Developer
Dibyendu Singha Roy is an experienced PHP developer with NetiApps. He also has expertise in Javascript, MySQL Database among others. He has a masters in Computer Science and loves the world of coding. In his spare time Dibyendu likes to catch up on his reading and sleep whenever possible.