CRUD (Create Read Update Delete) Example in Laravel 5.2 from Scratch

CRUD (Create Read Update Delete) Example in Laravel 5.2 from Scratch

In this post we will give you information about CRUD (Create Read Update Delete) Example in Laravel 5.2 from Scratch. Hear we will give you detail about CRUD (Create Read Update Delete) Example in Laravel 5.2 from ScratchAnd how to use it also give you demo for it if it is necessary.

In this post i going to give example how to create CRUD (Create Read Update Delete) application in laravel 5 from starting. In this step you have to just follow few step and you can create simple CRUD application. In this CRUD application after follow all output you can listing items, create new item, edit item and also delete item function. I used resource route, validation, listing etc for this crud application example.

After finish all step successfully, you will find output like as bellow image output.

Preview:

Step 1: Laravel Installation

If you haven’t installed laravel in your system then you have to run bellow command and get new Laravel project.



composer create-project --prefer-dist laravel/laravel blog

After clone laravel application, we also require to install laravelcollective/html for Form class, you can install from here : HTML/FORM not found in Laravel 5?.

Step 2: Create items table and model

In this step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:



php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.



use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateItemsTable extends Migration

{


public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}


public function down()

{

Schema::drop("items");

}

}

After craete “items” table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php



namespace App;


use IlluminateDatabaseEloquentModel;


class Item extends Model

{


public $fillable = ['title','description'];


}

Step 3: Add Route and Controller

Now we have to add route for items CRUD, in this example i added resource route for application, if we add resource route then it will add index, create, show, edit and delete route automatically. So add bellow line in your route file.

app/Http/routes.php



Route::resource('itemCRUD','ItemCRUDController');

Ok, now we should create new controller as ItemCRUDController in this path app/Http/Controllers/ItemCRUDController.php. this controller will manage all route method:

app/Http/Controllers/ItemCRUDController.php



namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpControllersController;

use AppItem;


class ItemCRUDController extends Controller

{


/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request)

{

$items = Item::orderBy('id','DESC')->paginate(5);

return view('ItemCRUD.index',compact('items'))

->with('i', ($request->input('page', 1) - 1) * 5);

}


/**

* Show the form for creating a new resource.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('ItemCRUD.create');

}


/**

* Store a newly created resource in storage.

*

* @param IlluminateHttpRequest $request

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


Item::create($request->all());

return redirect()->route('itemCRUD.index')

->with('success','Item created successfully');

}


/**

* Display the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function show($id)

{

$item = Item::find($id);

return view('ItemCRUD.show',compact('item'));

}


/**

* Show the form for editing the specified resource.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function edit($id)

{

$item = Item::find($id);

return view('ItemCRUD.edit',compact('item'));

}


/**

* Update the specified resource in storage.

*

* @param IlluminateHttpRequest $request

* @param int $id

* @return IlluminateHttpResponse

*/

public function update(Request $request, $id)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


Item::find($id)->update($request->all());

return redirect()->route('itemCRUD.index')

->with('success','Item updated successfully');

}


/**

* Remove the specified resource from storage.

*

* @param int $id

* @return IlluminateHttpResponse

*/

public function destroy($id)

{

Item::find($id)->delete();

return redirect()->route('itemCRUD.index')

->with('success','Item deleted successfully');

}

}


Step 4: Create Blade File

In this step we will create blade file for listing, craete, edit and show item and also create default blade file for theme setting. So first we create new layouts directory and create default.blade.php file inside that folder.

resources/views/layouts/default.blade.php



<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">


<title>Laravel CRUD</title>

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">


</head>

<body>


<div >

@yield('content')

</div>


</body>

</html>

Ok, now we will creae layout for items listing, So first we create new ItemCRUD directory and create index.blade.php file inside that folder.

resources/views/ItemCRUD/index.blade.php



@extends('layouts.default')


@section('content')


<div >

<div >

<div >

<h2>Items CRUD</h2>

</div>

<div >

<a href="{{ route('itemCRUD.create') }}"> Create New Item</a>

</div>

</div>

</div>


@if ($message = Session::get('success'))

<div >

<p>{{ $message }}</p>

</div>

@endif


<table >

<tr>

<th>No</th>

<th>Title</th>

<th>Description</th>

<th width="280px">Action</th>

</tr>

@foreach ($items as $key => $item)

<tr>

<td>{{ ++$i }}</td>

<td>{{ $item->title }}</td>

<td>{{ $item->description }}</td>

<td>

<a href="{{ route('itemCRUD.show',$item->id) }}">Show</a>

<a href="{{ route('itemCRUD.edit',$item->id) }}">Edit</a>

{!! Form::open(['method' => 'DELETE','route' => ['itemCRUD.destroy', $item->id],'style'=>'display:inline']) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}

{!! Form::close() !!}

</td>

</tr>

@endforeach

</table>


{!! $items->render() !!}


@endsection

Now we create new blade file for create new item, it’s call create.blade.php file inside “ItemCRUD”.

resources/views/ItemCRUD/create.blade.php



@extends('layouts.default')


@section('content')


<div >

<div >

<div >

<h2>Create New Item</h2>

</div>

<div >

<a href="{{ route('itemCRUD.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::open(array('route' => 'itemCRUD.store','method'=>'POST')) !!}

<div >


<div >

<div >

<strong>Title:</strong>

{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}

</div>

</div>


<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>


<div >

<button type="submit" >Submit</button>

</div>


</div>

{!! Form::close() !!}


@endsection

Next, we have to create show.blade.php file for item details page, so again create this file in ItemCRUD folder.

resources/views/ItemCRUD/show.blade.php



@extends('layouts.default')


@section('content')


<div >

<div >

<div >

<h2> Show Item</h2>

</div>

<div >

<a href="{{ route('itemCRUD.index') }}"> Back</a>

</div>

</div>

</div>


<div >


<div >

<div >

<strong>Title:</strong>

{{ $item->title }}

</div>

</div>


<div >

<div >

<strong>Description:</strong>

{{ $item->description }}

</div>

</div>


</div>


@endsection

At last, we need to create edit.blade.php file for update item, so again create this file in ItemCRUD folder.

resources/views/ItemCRUD/edit.blade.php



@extends('layouts.default')


@section('content')


<div >

<div >

<div >

<h2>Edit New Item</h2>

</div>

<div >

<a href="{{ route('itemCRUD.index') }}"> Back</a>

</div>

</div>

</div>


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::model($item, ['method' => 'PATCH','route' => ['itemCRUD.update', $item->id]]) !!}

<div >


<div >

<div >

<strong>Title:</strong>

{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}

</div>

</div>


<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>


<div >

<button type="submit" >Submit</button>

</div>


</div>

{!! Form::close() !!}


@endsection

Now, you can check…..

Video

Hope this code and post will helped you for implement CRUD (Create Read Update Delete) Example in Laravel 5.2 from Scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

36  +    =  45

We accept paid advance and paid guest Posting on our Site : Contact US