Laravel 7 CRUD Operation Tutorial

10-Apr-2023

.

Admin

Laravel 7 CRUD Operation Tutorial

This article will give you example of laravel 7 crud operation step by step. i explained simply about laravel 7 crud example. you will learn insert update delete with laravel 7. In this article, we will implement a crud operation laravel 7. Alright, let’s dive into the steps.

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

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 7.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 7

Step 1 : Install Laravel 7 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:

.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.

/database/migrations/2020_03_05_100722_create_items_table.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

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.

/app/Item.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model

{

protected $fillable = [

'title','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.

/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.

/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([

'title' => '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([

'title' => '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.

/resources/views/items/layout.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 7 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>

/resources/views/items/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 7 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->title }}</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

/resources/views/items/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 7 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="title" 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

/resources/views/items/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 7 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

@method('PUT')

<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="title" value="{{ $item->title }}" 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

/resources/views/items/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 7 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->title }}

</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...

#Laravel 7