Laravel 8 Crud Operation Step By Step. Save, Update Delete and View Record

Laravel 8 Crud Operation Step By Step With Example

In previous record you have learn how to use session in laravel 8. But today we learn Laravel 8 Crud Operation Step By Step with example. as you know laravel 8 already release with the many new features and functionality.

In laravel curd operation we use the controller and model and in this operation we start with a very basic example.

To make crud operation just follow thiese simple and easy step.

step 01: Create New project with the name of laravel crud example

Step 02: Open phpmyadmin and Create new database

Step 03: Open .env file and Configur Database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

Step 04: Open resources folder and create 3 views in views directory.

  1. adduser.blade.php
  2. edituser.blade.php
  3. userlist.blde.php

Step 05: Create on model with the name of users

php artisan make:model users

Step 06: Create controller for creating some crud functions.

php artisan make:controller UserController

Step 07: Open UserController and Use model in .

use App\MOdels\Users;

Step 08: Open web.php file which you can find it from routes directory. And include UserController in it.

use App\Http\Controllers\UserController;

Step 09: Open adduser.blade.php file and add below html code

{{-- Laravel 8 Crud Operation Step By Step --}}

@include('script')
<h1>User Signup</h1>
<div class="form-group frm">
    <form action="" method="POST">
        @csrf
        <input class="form-control" type="text" name="username" placeholder="Enter Username"><br><br>
        <input class="form-control" type="email" name="email" placeholder="Enter Email"><br><br>
        <input class="form-control" type="password" name="password" placeholder="Enter Password"><br><br>
        <div class= "form-group">
		<button class="btn btn-primary" type="submit">Submit</button>
		</div>
    </form>
</div>

<style>
.frm {

    margin-top: 20px;
    margin-left: 450px;
    margin-right: 450px;
}

h1 {
    text-align: center;
	color:blue;
    margin-top: 20px;
    margin-left: 450px;
    margin-right: 450px;

}
</style>

Step 10: Open userlist.blade.php file and add below code.

{{-- Laravel 8 Crud Operation Step By Step --}}
@include('script')
<!DOCTYPE html>
<html>

<head>
    <title>User List</title>
</head>

<body>
	<nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02"
            aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
                <li class="nav-item active">
                    <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/user">Add User</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/list">User List</a>
                </li>
            </ul>
            <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="search" placeholder="Search">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
        </div>
    </nav>
	<h1>User List</h1>
	<div class="table-responsive">
    <table class ="table" id="example" border="1">
        <thead>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th style="text-align:center; ">Action</th>
        </thead>
        <TR>
            @foreach ($userlist as $lst)
            <TD>{{$lst['id']}}</TD>
            <TD>{{$lst['username']}}</TD>
            <TD>{{$lst['email']}}</TD>
            <td style="text-align:center;"><a href='delete/{{ $lst["id"] }}'class="text-danger"><i class="fas fa-trash-alt"></i>Delete</a> ||
                <a href='edit/{{ $lst["id"] }}'>Edit</a>
				
            </td>
        </TR>
        @endforeach
    </table>
</div>
</body>

</html>
<style>
	th{
		color:blue;
	}
	h1{
		text-align:center;
		color:purple;
	}
	</style>

Step 11: Open edituser.blade.php file add below code.

{{-- Laravel 8 Crud Operation Step By Step --}}
@include('script')
<h1>User Update</h1>
<div class="form-group frm">
<form action="/edit" method="POST">
	@csrf
	<input class="form-control"type="hidden" name="id" value="{{$data['id']}}">
	<input class="form-control"type="text" name="username" value="{{$data['username']}}"><br><br>
	<input class="form-control"type="email" name="email" value="{{$data['email']}}"><br><br>
	<input class="form-control"type="password" name="password" value="{{$data['password']}}"><br><br>
	<button class="btn btn-success" type="submit">Update</button>
</form>
</div>
<style>
.frm {

    margin-top: 20px;
    margin-left: 450px;
    margin-right: 450px;
}

h1 {
    text-align: center;
	color:blue;
    margin-top: 20px;
    margin-left: 450px;
    margin-right: 450px;

}
</style>

Step 12: Open UserController.php file and add the follwoing functions.

function AddUser(Request $req){
 
    	$user = new Users;
    	$user->username=$req->username;
    	$user->email=$req->email;
    	$user->password=$req->password;
    	$user->save();
    	return redirect ('list');

    }
     function show(){
    	//return DB::select("select * From courses");
    	$data=Users::all();
    	return view('userlst',['userlist'=>$data]);

    }
    function delete($id){
    	$data=Users::find($id);
    	$data->delete();
    	return redirect('list');

    }
     function showdata($id){
    	
    	$data= Users::find($id);
    	return view('updateuser',['data'=>$data]);

    }
   function update(Request $req){
    	$data=Users::find($req->id);
    	$data->username=$req->username;
    	$data->email=$req->email;
    	$data->password=$req->password;
    	$data->save();
    	return redirect('list');

    }
Step 13: Open web.php file add the routes.
Route::get('/', function () {
    return view('index');


});
Route::view('home','home');
//save data in data table
Route::view('user','addusers');
Route::post('user',[UserController::class,'AddUser']);

//show data in html table
Route::view('list','userlst');
Route::get('list',[UserController::class,'show']);
//delete data from table
Route::get('delete/{id}',[UserController::class,'delete']);

//route for update
Route::view('edit','updateuser');
Route::get('edit/{id}',[UserController::class,'showdata']);
Route::post('edit',[UserController::class,'update']);
Laravel 8 Crud Operation Step By Step
Laravel 8 Crud Operation Step By Step

Leave a Reply