Beyond the boundaries

Easy life with Laravel

When it comes to selecting a framework to use, there are plenty of options out there. Sometimes we may be unable to find a 100% matching framework for our project but it's in our hand to select the most suitable one for our project because most of these frameworks are good in their own way and have unique characteristics. So, in my blogpost I’m not going to tell you how laravel made my life easy, rather than implying that laravel is better than codeigniter or any other framework.

Here, I’ll  discuss about 4 major features in laravel which codeigniter lack. Those are,

  • Artisan
    A command line interface tool.
  • Eloquent
    Object relational mapping tool.
  • Faker
    Database seed tool.
  • Composer
    Dependency management tool.

Artisan

If we look at our work log for a given project we can point out few actions which we use frequently like creating a controller, creating a data modal and even a database migration. To complete each of above tasks we need to follow a certain procedure repeatedly for each of them. For example let’s assume that to make a controller we need to carry out the following steps.

  1. Create the php file with controller name.
  2. Write the class.
  3. Extend it from ‘Controller” class.

So for each and every controller we are making, we have to follow the above steps and it is a waste of time.

Laravel identified this and it has supplied a handy tool called Artisan, a command line interface tool. It provides a number of helpful commands that can assist you while you build your application.

By using artisan creating a simple controller or model is just one command.

//Making a controller
php artisan make:controller ProductController

//Making a controller
php artisan make:modal Product

Eloquent

Dealing with databases, queries and handling SQL exceptions is no more a trouble with laravel. Because it’s a great Object Relational Mapping (ORM) tool integrated within itself.

What is this ORM ?

There are some programmers who need to apply OOP concepts into their web development. Hence some vendors come up with some tools to backup them like typescript for javascript. This ORM concept comes with this approach. Basically it creates a relationship between model class and the model’s table in a way that the program implicitly recognize the corresponding table  when dealing with the model.

As mentioned, Eloquent is the ORM tool for laravel and it ease’s our life in many ways. We can point out the characteristics of Eloquent as follows.

  • Executing query in a OOP manner.
  • Easy to use than raw query or query builder.
  • No binding with table schema. Relationship among tables can be maintain in an elegant way.
  • Queries highly readable while written using Eloquent comparing with Query Builder.
  • Protected from sql injection.

By using Eloquent we can run most common queries with one method call like below.

$flight = App\Flight::where('number', 'FR 900')->first();

// Retrieve a model by its primary key...
$flight = App\Flight::find(1);

// Delete a model by its primary key...
App\Flight::destroy(collect([1, 2, 3]));

Faker

Adding multiple records to the database writing them one by one takes away a lot of valuable time. When it comes to large databases seed the database with correct and relevant data for testing purposes is time consuming than anything. We have to consider about constraints, relationships and also relativity of the data.

https://unsplash.com/@cjoudrey

So the faker in database seed tool for PHP users and laravel comes with it. So, let’s understand this with a example. Assume that you need to create 10 test users. Normally, we have to enter them manually. Entering 10 users is easy, but think about entering 1000 users for performance testing purposes. Is it practical to enter them manually?

So, here is the laravel faker way to do that.

First create the seeder class.

use Illuminate\Database\Seeder;

class CustomersTableSeeder extends Seeder
{
   /**
    * Run the database seeds.
    *
    * @return void
    */

   public function run()
   {
       $faker = Faker\Factory::create();

       $limit = 33;

       for ($i = 0; $i < $limit; $i++) {
           DB::table('customers')->insert([ //,
               'name' => $faker->name,
               'email' => $faker->unique()->email,
               'contact_number' => $faker->phoneNumber,
           ]);
       }
   }
}

Then run the php artisan db:seed in your command line. Now you get 10 users instantly. So, like that we can seed our database with related, correct data easily.

Faker can be used to generate the following data types

  1. Numbers
  2. Lorem text
  3. Person i.e. titles, names, gender etc.
  4. Addresses
  5. Phone numbers
  6. Companies
  7. Text
  8. DateTime
  9. Internet i.e. domains, URLs, emails etc.
  10. User Agents
  11. Payments i.e. MasterCard
  12. Colour
  13. Files
  14. Images
  15. uuid
  16. Barcodes
  17. Miscellaneous

Composer

All above 3 tools which I mentioned about are separate modules which were created by either the laravel team or some other third party vendors. We might have to use other modules except to above default packages when our project grows. For managing these dependencies laravel gets support from composer, a dependency manager for laravel. It’s like NPM (node package manager) for nodejs. Because of these tools we just needed to install our required dependencies and then composer will take care of the other parts like managing its dependencies and update them.

Conclusion

There are lot of frameworks out there to use. Some of them are free and some are for commercial use. It is our responsibility to choose the most matching framework before starting the project after analyzing the requirements well. Because most of the frameworks are good in their own way or ideal for a certain task.  

Dilusha Dasanayaka

17-01-2019

Responces

Post comment