FahmidasClassroom

Learn by easy steps

Laravel Crud

Steps:

1. Create a new Laravel Project
2. Install html package using Laravel

composer require laravelcollective/html

3. Change app.php file

Collective\Html\HtmlServiceProvider::class,
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class


4. Create a new view file welcome2.blade.php by Copying default welcome.blade.php file.
5. Replace the code of body with the following code,

<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="links">
 <a href="{{ url('/lists') }}">show list</a>
</div>
</div>
</div>

6. Create master blade file in layouts folder by attaching bootstap files. [this is shown in another tutorial]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Laravel</title>
<link rel="stylesheet" href="/css/bootstrap.css">
<script type="text/javascript" src='/js/bootstrap.min.js'></script>
<script type="text/javascript" src="/js/jquery-2.2.4.min.js"></script>
 
</head>
<body>
<div class="container">
<div class="col-md-12">
@yield('content')
 
@if(Session::has('message'))
 
<div class="alert alert-info">
{{ Session::get('message') }}
</div>
@endif
</div>
</div>
</body>
</html>

7. Create a resource controller

php artisan make:controller --resource ListsController


8. Create a model for items table

php artisan make:model Item

9. Add the following variable in model class

protected $fillable=['name','price'];

10. Create a form request class for form validation

php artisan make:request ListFormRequest


11. Modify ListFormRequest.php file

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ListFormRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name'=>'required',
'price'=>'required',
];
}
}


12. Replace Listscontroller with the following code

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//Add
use App\Http\Requests;
use App\Http\Requests\ListFormRequest;
use App\Item;
class ListsController extends Controller
{
 
public function index()
{
$lists = Item::all();
return view('lists.index')->with('lists',$lists);;
}
 
public function create()
{
return view('lists.create');
}
public function store(ListFormRequest $request)
{
 
$list=new Item(array('name'=>$request->get('name'),
'price'=>$request->get('price'), ));
$list->save();
return \Redirect::route('lists.index')->with('message','Your list has been created!');
}
 
public function edit($id)
{
$list=Item::find($id);
return view('lists.edit')->with('list',$list);
}
public function update(Request $request, $id)
{
$list=Item::find($id);
 
$list->update(['name'=>$request->get('name'),
'price'=>$request->get('price'),
 
]);
 
$list->save();
return \Redirect::route('lists.index',array($list->id))->with('message','Your list has been updated!');
}
public function destroy($id)
{
Item::destroy($id);
return \Redirect::route('lists.index')->with('message','The list has been deleted!');
}
}

13. Open web.php file and add the following route


Route::resource('lists','ListsController');

Route::get('/lists/{id}/delete','ListsController@destroy');

14. Create index.blade.php file and add the following code

@extends('layouts.master')
@section('content')
<br/>
<h1>All List</h1>
<table class="table" >
<tr><th>Id</th><th>Name</th><th>Price</th><th>Actions</th></tr>
@foreach($lists as $list)
@php
$id = $list->id
@endphp
<tr><td>{{ $list->id }}</td><td>{{ $list->name }}</td><td>{{ $list->price }}</td>
<td><a href="{{ url('/lists/create') }}">Add</a> &nbsp;
<a href="{{ url('/lists/'.$id.'/edit') }}">Edit</a> &nbsp;
<a href="{{ url('/lists/'.$id.'/delete') }}">Delete</a></td></tr>
@endforeach
@endsection

15. Create create.blade.php file and add the following code


@extends('layouts.master')
@section('content')
<h1>Create a New List</h1>
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
{!!Form::open(array('route'=>'lists.store','class'=>'form'))!!}
<div class="form-group">
{!!Form::label('Item Name')!!}
{!!Form::text('name',null,array('required','class'=>'form-control','placeholder'=>'Cake'))!!}
</div>
<div class="form-group">
{!!Form::label('Item Price')!!}
{!!Form::text('price',null,array('required','class'=>'form-control','placeholder'=>'100'))!!}
</div>
<div class="form-group">
{!!Form::submit('Create List',array('class'=>'btn btn-primary'))!!}
</div>
{!!Form::close()!!}
@stop

16. Create edit.blade.php file and add the following code


@extends('layouts.master')
@section('content')
<h1>Edit List</h1>
{!!Form::model($list,array('method'=>'put','route'=>['lists.update',$list->id],'class'=>'form'))!!}
<div class="form-group">
{!!Form::label('List Name')!!}
{!!Form::text('name',null,array('required','class'=>'form-control','placeholder'=>'Cake'))!!}
</div>
<div class="form-group">
{!!Form::label('List Price')!!}
{!!Form::text('price',null,array('required','class'=>'form-control','placeholder'=>'200'))!!}
</div>
<div class="form-group">
{!!Form::submit('Update List',array('class'=>'btn btn-primary'))!!}
</div>
{!!Form::close()!!}
@stop
 
You can check the following video link to show the output of this tutorial.

You can also check the another crud tutorial on Laravel 7 from the following link.

Laravel 7 CRUD using Bootstrap Modal