How Laravel Helps Me Code Less...

Dibyendu Singha Roy

PHP Developer

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 programmers 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 a 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 how we can achieve this in PHP

To check the mobile_number is empty or not, we can do this

  1. if(empty($_POST[‘mobile_number’])){}

Now if somebody enters blank space then empty() won’t work in that case we need to use trim()

  1. 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();

  1. 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

  1. if(!is_number($_POST[‘mobile_number’])){ }

Now it should be of 10 digits only and for that we need to use strlen() function

  1. 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

  1. 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 a 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 same mobile number so you have to make it unique.

This is a big challenge in custom php. You need to write query to check whether any user exists with the same mobile_number then check the number of rows, then if row count is 0 proceed else 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. Please Contact us to know more about Laravel Application Development.

 

Author Bio

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.