Laravel Installation and Quick Start Guide
Laravel Quick Start
- Hope you have installed composer the php package manager.
- composer create-project --prefer-dist laravel/laravel LaravelQuickStart
- Create Database laravelquickstart in Mysql
- Open .env file and add db params
- 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
- php artisan tinker
- Create a user.
- User::create(['name'=>'admin','email'=>'admin@gmail.com','password'=>bcrypt('123456')]);
- Browse to http://localhost/LaravelQuickStart/public/login
- Login with email and password
Creating a Todo App
Creating Todo controller and Routes together
- 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.
- Open \LaravelQuickStart\routes\web.php and add Route::resource('todos', 'TodoController'); // register a resourceful route to the controller
- 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
- php artisan make:model Todo -m // Generates model and migration
- Open \LaravelQuickStart\database\migrations\2017_08_17_143816_create_todos_table.php
- In up method add
- $table->increments('id');
- $table->string('text');
- $table->boolean('completed');
- $table->integer('user_id')->unsigned();
- $table->timestamps();
- In down add
- Schema::dropIfExists('todos');
- php artisan migrate
Todo controller
- Open \LaravelQuickStart\app\Http\Controllers\TodoController.php.
- __construct will load the model
- index function will list all the todos http://localhost/LaravelQuickStart/public/todos
- create function will show the add form http://localhost/LaravelQuickStart/public/todos/create
- store function will save the records into db.
- show function will show the details of a single todo.
- edit function will show the edit form a todo.
- update will update the todo by its $id field.
- destroy will delete the todo by its $id field.
- 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
- List the todos by looping through it.
- 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.
- Check the html here
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.');
}
- Create a folder todo in \LaravelQuickStart\resources\views\
- Copy the file login.blade.php blade from LaravelQuickStart\resources\views\auth
- Rename it to create.blade.php
- Make the todo form. See Here
- Action of the form should be given as {{ route('todos.store') }}
- Open \LaravelQuickStart\resources\views\layouts\app.blade.php
- 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
Post a Comment