LxD Series: Laravel Basics

Before we dive into coding Laravel based app, let’s learn what’s it all about.

Laravel is an MVC based framework for rapid PHP based web application development. MVC(Model-View-Controller) is a design pattern used to write softwares. It consist of three parts:

  • Model: This component deals with data and it’s manipulation. Usually you deal with database(RDBMS,NoSQL) here but this is not necessary at all. Your model could interact with a flat file or some remote API for data manipulation.
  • View: The data which was processed or manipulated on model layer is shown by Views to end-users so that they could interact with the app.
  • Controller: Controller actually sits between View and Model and works as a bridge between two. Controller could be used to processed the data you fetch from Model and make it presentable to view as per your need.

Routing

In simplest word Routing is a mechanism used to map one kind of URL to another. Routing is used to convert ugly looking URLs to user-friendly or SEO friendly URL. So if there’s a URL http://example.com/users/user?id=12&name=Jhon, routing can convert it to http://exampe.com/users/user/12/Jhon. Prior to MVC frameworks we used to write multiple rules to cater different kind of URLS. MVC frameworks like Larave provides an easy way to come up a URL format as per your need. Behind the scene it still uses .htacess file. Laravel takes the benefit of Closure to implement routing.

Migrations

Usually when you create a database driven app you will create a sql script to create tables in db. Later when you go in production you have to run script on remote server. While working in a team and multiple developers making changes in a single database. It gets difficult to keep a track of it. Migrations help you to overcome this issue. You create migration files which contains timestamp in file name which contains an expressive syntax to create/drop tables. You can run a certain migration command on your local machine or remote server that will execute script in order.

Laravel provides easy to use command to perform various migration related operations. For instance:

php artisan migrate:make create_users_table

Will create a migration file that will be responsible to create a table users. It will create a file like this:

public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('name');
        $table->timestamps();
    });
}

public function down()
{
    Schema::drop('users');
}

create creates a table users while down drops it.

That’s all folks. In coming post we will be implementing routes and design in our Laravel app. Stay tuned!

 

 

Join with me and let’s learn together!

* indicates required