CRUD Operations in Laravel 6 Example

Laravel 6 , Laravel

Nicesnippets

1369

30-01-2020


Hi Guys,

Today ,i will explain crud operations in laravel 6. we will show simply example of crud operations in laravel 6. i will give you step by step instruction for creating crud (Create Read Update Delete) Application in laravel 6.you will understand how to use resource route, controller, blade files, model and migration for crud operation in laravel 6.

You just need to follow few step and you will get basic crud stuff using controller, model, route, bootstrap 4 and blade..

In this example,you will learn very basic crud operation with laravel new version 6.we are going to show you step by step from scratch so, i will better to understand if you are new in laravel.

Here following step of crud operations in laravel 6

Step 1 : Install Laravel 6 Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Database Configuration

In this step, we require to make database configuration, you have to add following details on your .env file.

1.Database Username

1.Database Password

1.Database Name

In .env file also available host and port details, you can configure all details as in your system, So you can put like as bellow:

following path: .env

DB_HOST=localhost

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

Step 2: Create items Table and Model

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

php artisan make:model Item -m

After this command you have to put bellow code in your migration file for create items table.

following path: /database/migrations/2020_01_10_102325_create_items_table.php

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('items');

}

}

Now we require to run migration be bellow command:

php artisan migrate

After you have to put bellow code in your model file for create items table.

following path:/app/Item.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model

{

protected $fillable = [

'name','description'

];

}

Step:3 Create Route

now, we need to add resource route for item crud operations in laravel 6 application. so open your "routes/web.php" file and add following route.

following path:/routes/web.php/

Route::resource('items','ItemController');

Step:4 Create Controller

here this step now we should create new controller as ItemController. So run bellow command and create new controller.

php artisan make:controller ItemController

In this controller will create seven methods by default as bellow methods:

1)index()

2)create()

3)store()

4)show()

5)edit()

6)update()

7)destroy()

So, let's copy bellow code and put on ItemController.php file.

following path: /app/Http/Controllers/ItemController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Yajra\Datatables\Datatables;

use App\Item;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$items = Item::latest()->paginate(5);

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

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

}

/**

* Show the form for creating a new resource.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('items.create');

}

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'name' => 'required',

'description' => 'required',

]);

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

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

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

}

/**

* Display the specified resource.

*

* @param \App\Item $Item

* @return \Illuminate\Http\Response

*/

public function show(Item $item)

{

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

}

/**

* Show the form for editing the specified resource.

*

* @param \App\Item $Item

* @return \Illuminate\Http\Response

*/

public function edit(Item $item)

{

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

}

/**

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param \App\Item $Item

* @return \Illuminate\Http\Response

*/

public function update(Request $request, Item $item)

{

$request->validate([

'name' => 'required',

'description' => 'required',

]);

$item->update($request->all());

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

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

}

/**

* Remove the specified resource from storage.

*

* @param \App\Item $Item

* @return \Illuminate\Http\Response

*/

public function destroy(Item $item)

{

$item->delete();

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

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

}

}

Step: 5 Create View

In last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder "items" then create blade files of crud app. So finally you have to create following bellow blade file:

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

here create following file and put bellow code.

layout.blade.php

following path: /resources/views/products/layout.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 6 CRUD Application</title>

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

</head>

<body class="bg-dark">

<div class="container">

<div class="row">

<div class="col-md-12">

@yield('content')

</div>

</div>

</div>

</body>

</html>

index.blade.php

following path: /resources/views/products/index.blade.php

@extends('items.layout')

@section('content')

<div class="card mt-5">

<div class="card-header">

<div class="col-md-12">

<h4 class="card-title"> CRUD Operations in Laravel 6 Example - nicesnippets.com

<a class="btn btn-success ml-5" href="{{ route('items.create') }}" id="createNewItem"> Create New Item</a>

</h4>

</div>

</div>

<div class="card-body">

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

<div class="alert alert-success">

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

</div>

@endif

<table class="table table-bordered">

<tr>

<th width="5%">No</th>

<th>Name</th>

<th>Description</th>

<th width="20%">Action</th>

</tr>

@foreach ($items as $item)

<tr>

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

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

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

<td>

<form action="{{ route('items.destroy',$item->id) }}" method="POST">

<a class="btn btn-info btn-sm" href="{{ route('items.show',$item->id) }}">Show</a>

<a class="btn btn-primary btn-sm" href="{{ route('items.edit',$item->id) }}">Edit</a>

@csrf

@method('DELETE')

<button type="submit" class="btn btn-danger btn-sm">Delete</button>

</form>

</td>

</tr>

@endforeach

</table>

</div>

@endsection

create.blade.php

following path: /resources/views/products/create.blade.php

@extends('items.layout')

@section('content')

<div class="card mt-5">

<div class="card-header">

<div class="col-md-12">

<h4 class="card-title"><strong>Create Page</strong> CRUD Operations in Laravel 6 Example - nicesnippets.com

<a class="btn btn-success ml-5" href="{{ route('items.index') }}">Back</a>

</h4>

</div>

</div>

<div class="card-body">

@if ($errors->any())

<div class="alert alert-danger">

<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 action="{{ route('items.store') }}" method="POST">

@csrf

<div class="row">

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Name:</strong>

<input type="text" name="name" class="form-control" placeholder="Name">

</div>

</div>

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Description:</strong>

<textarea class="form-control" style="height:150px" name="description" placeholder="Description"></textarea>

</div>

</div>

<div class="col-xs-12 col-sm-12 col-md-12 text-center">

<button type="submit" class="btn btn-primary">Submit</button>

</div>

</div>

</form>

</div>

@endsection

edit.blade.php

following path: /resources/views/products/edit.blade.php

@extends('items.layout')

@section('content')

<div class="card mt-5">

<div class="card-header">

<div class="col-md-12">

<h4 class="card-title"><strong>Edit Page</strong> CRUD Operations in Laravel 6 Example - nicesnippets.com

<a class="btn btn-success ml-5" href="{{ route('items.index') }}">Back</a>

</h4>

</div>

</div>

<div class="card-body">

@if ($errors->any())

<div class="alert alert-danger">

<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 action="{{ route('items.update',$item->id) }}" method="POST">

@csrf

<div class="row">

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Name:</strong>

<input type="text" name="name" value="{{ $item->name }}" class="form-control" placeholder="Name">

</div>

</div>

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Description:</strong>

<textarea class="form-control" style="height:150px" name="description" placeholder="Description">{{ $item->description }}</textarea>

</div>

</div>

<div class="col-xs-12 col-sm-12 col-md-12 text-center">

<button type="submit" class="btn btn-primary">Submit</button>

</div>

</div>

</form>

</div>

@endsection

show.blade.php

following path: /resources/views/products/show.blade.php

@extends('items.layout')

@section('content')

<div class="card mt-5">

<div class="card-header">

<div class="col-md-12">

<h4 class="card-title"><strong>Show Page</strong> CRUD Operations in Laravel 6 Example - nicesnippets.com

<a class="btn btn-success ml-5" href="{{ route('items.index') }}">Back</a>

</h4>

</div>

</div>

<div class="card-body">

<div class="row">

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Name:</strong>

{{ $item->name }}

</div>

</div>

<div class="col-xs-12 col-sm-12 col-md-12">

<div class="form-group">

<strong>Description:</strong>

{{ $item->description }}

</div>

</div>

</div>

</div>

@endsection

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/items

It will help you...


Recommended Posts