A Step-by-Step Guide to Getting Started With Laravel Framework

A Step-by-Step Guide to Getting Started With Laravel Framework

Laravel framework development is certainly a complex job which requires one to attain proper knowledge of various steps involved to complete this process. Our step by step guide is helpful for those who are new to the Laravel framework.

Introduction –

Although, Laravel framework is based on MVC architecture which is a traditional design pattern, yet there are several things and features that make it an advanced framework of the time. So, you should be aware of all these features before getting started with this framework for any of your web project.
As Laravel uses MVC architecture, it requires the developers to use:

  • Models – It is used to communicate with the database and recover the information on your objects.
  • Views – to extract pages
  • Controllers – To manage the requests of the users and recover data by influencing models.

Apart from above-mentioned aspects, there are routes which are utilized to map the URLs to dedicated controller actions

A complete request cycle of MVC in Laravel application –

Request – Request is made when the user puts a URL related to your web application.

Route – A route related to that URL plots the URL to the controller action.

Controller – Then, controller action encourages the required model to recover information from the database and after that passes the data to the view.

View – the view extracts the finally designed page.

Getting Started To Make A Web Application Using Laravel Framework –

As Laravel framework is based on MVC pattern, so to make a web application, you have to cover each component like model, view and controller. So, for web application development on Laravel framework, first step is:

Model –

Start your development by creating the model of your project. The Laravel framework accompanies a brilliant, inbuilt command line web interface namely Artisan CLI. This interface offers the programmers with several useful commands for making web applications. Therefore, shoot up the command line and move into the main directory of your application, and then, run the command as below to make the “Model”.

$ php artisan make:model abc --migration

Once models are created, they will keep on storing in the primary app directory and thus, the app/abc.php model file will be generated by command with this code:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class abc extends Model
{
    //
}

As Laravel has inbuilt functionality of model, just by making a hollow model class, it assumes that the model is related to the database table namely abc. Though, in reality, by adding that –migration, while creating model, this framework also created a migration file for database for making database table for abc. Then, that migration file is accommodated at [timestamp]_create_abc_table.php. There is following code with the migration file.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateabcTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    /
    public function up()
    {
    Schema::create('abc', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
    }
    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::drop('abc');
    }
}

For the completion of your migration file, you just have to use the Schema builder Laravel documentation which helps defining some extra columns to, say, store the abc’ make, model, and production date:

.
.
.
Schema::create('abc', function (Blueprint $table) {
$table->increments('id');
$table->string('make');
$table->string('model');
$table->date('produced_on');
$table->timestamps();
});
.
.
.

After that, you can easily run migration by using Artisan command for creating the abc table:

$ php artisan migrate

With the addressed items of the database, now, you can move on creating the controller which is the next step of Laravel framework development.

Controller –

In Laravel framework, an object type like abc is considered as the resource. Though it is quite common to create applications across resources, one can easily create resource controller which is a controller to manage all requests associated with a resource. For this purpose, you should use following Artisan command:

$ php artisan make:controller abcController

This command will create a controller file app/Http/Controllers/AbcController.php along with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AbcController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return Response
    */
    public function index()
    {
        //
    }
    /**
    * Show the form for creating a new resource.
    *
    * @return Response
    */
    public function create()
    {
        //
    }
    /**
    * Store a newly created resource in storage.
    *
    * @return Response
    */
    public function store()
    {
        //
    }
    /**
    * Display the specified resource.
    *
    * @param int $id
    * @return Response
    */
    public function show($id)
    {
        //
    }
    /**
    * Show the form for editing the specified resource.
    *
    * @param int $id
    * @return Response
    */
    public function edit($id)
    {
        //
    }
    /**
    * Update the specified resource in storage.
    *
    * @param int $id
    * @return Response
    */
    public function update($id)
    {
        //
    }
    /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return Response
    */
    public function destroy($id)
    {
        //
    }
}

It is worth to be mentioned that Laravel automatically creates the “Controller” using these CRUD typical actions. After that, you should definite routes for associating URLs to controller actions.

Routes –

In the configuration file of the route which is app/Http/routes.php, include the following command for defining the route for abc resource:

Route::resource('abc', 'abcController');

With this single definition of the route, all the routes associated with abc resource will automatically be defined.

Request Type Path Action Route Name
GET /abc Index abc.index
GET /abc/create create abc.create
POST /abc Store abc.store
GET /abc/{abc} Show abc.show
GET /abc/{abc}/edit Edit abc.edit
PUT/PATCH /abc/{abc} update abc.update
DELETE /abc/{abc} destroy abc.destroy

As per the given examples, now, we will work on the implementation for show action of ABC page:

Show Action In The Controller –

According to the table above, the show action of ABC page can be accessed at http://app.url/abc/{abc}. Here, the {abc} will act like an id for the abc object in your database. Thus, the URL for viewing the abc with id1 will be http://app.url/abc/1.

So, to implement the abc show action page, in the controller, you should follow these steps:

Take the abc model to recover the designated abc object in the database.

Load the view for a page of show abc, and then, pass it to the abc object recovered from database.

To access the abc model from controller, you need to include the new “use” statement over the class of controller:

.
.
.
use App\abc;
class AbcController extends Controller
{
.
.
.

After that, you can complete the show abc action following this command:

.
.
.
    public function show($id)
    {
      $abc = Abc::find($id);
      return view('abc.show', array('abc' => $abc));
    }
.
.
.

On seeing the URL of abc 1 – http://app.url/abc/1, the laravel framework creates “1” in the accessible URL through $id as mentioned in the abc show function as above. Recovering the abc object with the abc model is also quite simple as you say abc:: find and then, passing it in $id.

Then, view is packed with the function “view” and passing any name to view using a variety of data to be added to view.

In the end, we should create show abc view.

View & Page Completion –

All the view files in Laravel gets stored in folder of resources/views. You can organized them in different subfolders in your directory.
In last step, the view function was passed the name for view abc.show. it shows Laravel framework to check for the view file included in the subfolder namely abc in the main directory resources/views with the name show.blade.php. The view files in Laravel work on Blade Templating engine and so are called .blade.php.
Therefore, to complete implementation of this page, you should make the view file resources/views/abc/show.blade.php with this code:

<!DOCTYPE html>
<html>
  <head>
    <title>abc {{ $abc->id }}</title>
  </head>
  <body>
    <h1>abc {{ $abc->id }}</h1>
    <ul>
      <li>Make: {{ $abc->make }}</li>
      <li>Model: {{ $abc->model }}</li>
      <li>Produced on: {{ $abc->produced_on }}</li>
    </ul>
  </body>
</html>

As the abc object is passed to view, we should return back to the action Show in controller again with key array abc that can be acceded in view though variable of the name, $abc.
Objects taken from the model are examples of the class of that model. In the end, use a Blade syntax to print the information with following code for printing abc make.

{{ $abc->make }}

It is completely translated in the PHP echo in the background as below:

<?php echo $abc->make; ?>

We hope this tutorial will help you get a better idea about how to getting started with Laravel. If you still have a question in your mind, do let us know by commenting below in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *