Laravel Installation and Quick Start Guide

Laravel Quick Start

For those who are searching for a way to install and configure Laravel in right way can be go through following steps. At the time of writing Laravel version is Laravel Framework 5.4.33. You can download the full source code from here. This is all about quick way of doing. Good Luck :)
  1. Hope you have installed composer  the php package manager.
  2. composer create-project --prefer-dist laravel/laravel LaravelQuickStart
  3. Create Database laravelquickstart in Mysql
  4. Open .env file and add db params
  5. php artisan migrate
    • If it throws any error like "Specified key was too long; max key length is 1000 bytes". Open \LaravelQuickStart\app\Providers\AppServiceProvider.php add following lines
    • use Illuminate\Support\Facades\Schema;
    • In boot method add Schema::defaultStringLength(191);
    • Run php artisan migrate
  6. php artisan tinker
  7. Create a user.
  8. User::create(['name'=>'admin','email'=>'admin@gmail.com','password'=>bcrypt('123456')]);
  9. Browse to http://localhost/LaravelQuickStart/public/login
  10. Login with email and password

Creating a Todo App

Creating Todo controller and Routes together

  1. php artisan make:controller TodoController --resource // Generates a controller at app/Http/Controllers/TodoController.php. The controller will contain a method for each of the available resource operations.
  2. Open \LaravelQuickStart\routes\web.php and add Route::resource('todos', 'TodoController'); // register a resourceful route to the controller
  3. Following are the available routes of a resource controller https://laravel.com/docs/5.4/controllers#resource-controllers
    • GET  /todos index todos.index // Browse the URL http://localhost/LaravelQuickStart/public/todos
    • GET /todos/create create todos.create
    • POST /todos store todos.store
    • GET /todos/{todo} show todos.show // Here todo means explicit model route binding: in route service provider Route::model('todo', Todo::class);
    • GET /todos/{todo}/edit edit todos.edit
    • PUT/PATCH /todos/{todo} update todos.update
    • DELETE /todos/{todo} destroy todos.destroy

Creating Todo Model and Table together

  1. php artisan make:model Todo -m // Generates model and migration
  2. Open \LaravelQuickStart\database\migrations\2017_08_17_143816_create_todos_table.php
  3. In up method add
    • $table->increments('id');
    • $table->string('text');
    • $table->boolean('completed');
    • $table->integer('user_id')->unsigned();
    • $table->timestamps();
  4. In down add
    • Schema::dropIfExists('todos');
  5. php artisan migrate

Todo controller

  1.  Open \LaravelQuickStart\app\Http\Controllers\TodoController.php.
  2. __construct will load the model
  3. index function will list all the todos http://localhost/LaravelQuickStart/public/todos
  4. create function will show the add form http://localhost/LaravelQuickStart/public/todos/create
  5. store function will save the records into db.
  6. show function will show the details of a single todo.
  7. edit function will show the edit form a todo.
  8. update will update the todo by its $id field.
  9. destroy will delete the todo by its $id field.
  10. See TodoController

List Todo

index function

/**


   * Display a listing of the resource.

   *
   * @return \Illuminate\Http\Response
   */
    public function index() {
        $todos = $this->todo->all();
        return view('todo.index', ['todos' => $todos]);
    }
View
  1. List the todos by looping through it.
  2. Each to do will have a update form whose method is POST but Laravel method_field is PUT. We have a resource route defined for updating. 
  3. Check the html here
Create Todo

create function

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
*/
public function create() {
       return view('todo.create');
}

store function

/**

  * Store a newly created resource in storage.

  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */

public function store(Request $request) {



        $this->todo->text = $request->text;
        $this->todo->completed = 0;
        $this->todo->user_id = 1;
        if (isset($request->completed)) {
            $this->todo->completed = 1;
        }
        $this->todo->save();
        return redirect()->route('todos.create')->with('success', 'Todo has been created successfully.');
 }

View
  1. Create a folder todo in \LaravelQuickStart\resources\views\
  2. Copy the file login.blade.php blade from LaravelQuickStart\resources\views\auth
  3. Rename it to create.blade.php
  4. Make the todo form. See Here
  5. Action of the form should be given as {{ route('todos.store') }}
  6. Open \LaravelQuickStart\resources\views\layouts\app.blade.php
  7. Before the line @yield('content') put the following html. This is for showing alert messages.
    •  <div class="row">
    •                 <div class="col-md-4 col-md-offset-4">
    •                     <!-- /.panel-heading -->
    •                     <div class="panel-body">
    •                         @if (session('success'))
    •                         <p></p>
    •                         <div class="alert alert-success">
    •                             {!! session('success') !!}
    •                         </div>
    •                         @endif
    •                         @if (session('error'))
    •                         <p></p>
    •                         <div class="alert alert-danger">
    •                             {!! session('error') !!}
    •                         </div>
    •                         @endif
    •                         <!-- Page Content -->
    •                     </div>
    •                     <!-- /.panel-body -->
    •                     <!-- /.panel -->
    •                 </div>
    •                 <!-- /.col-lg-12 -->
    •             </div>

Comments