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