Hello Laravel Developers,
Today I am here with a Laravel CRUD operation step by step tutorial. In this guide, you will learn How to perform CRUD operation in Laravel 8 with an example.
CRUD refers to four basic functions of persistent storage: Create, Read, Update, Delete. It is the operations performed in the database.
You can also create CRUD in Laravel. CRUD operations Laravel is a simple task to perform even for beginners. Laravel CRUD operation can be easily performed with the help of the following steps or you can Hire an experienced Laravel Developer to help you perform the CRUD operation in Laravel 8.
Let’s dive into the process of the Laravel 8 CRUD generator.
Contents
Steps to Perform CRUD Operation in Laravel 8:
Step 1: Install Laravel 8
For the Laravel 8 CRUD generator process, first you need to install Laravel 8 with the help of the following command
1 | composer create-project laravel/laravel:^8.0 Laravel |
Step 2: Database configuration
Add following lines in .env file
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name(blog) DB_USERNAME=here database username(root) DB_PASSWORD=here database password |
Step 3: Create a table
1 | php artisan make:migration create_users_table --create table |
Now open the migration file and add the code given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
Now you have to run migration using the below command
1 | php artisan migrate |
Step 4: Add Resource Route
We need to add a resource route for the user crud application. So open your “routes/web.php” file and add the following route.
1 2 | use App\Http\Controllers\UserController; Route::resource('users', UserController::class); |
Step 5: Add Controller and Model
1 | php artisan make:controller UserController --resource --model=User |
After the above command, you will find a new file in this path
“app/Http/Controllers/UserController.php”
In this controller will create seven methods by default as given below:
- index()
- create()
- store()
- show()
- edit()
- update()
- destroy()
Go to the path: app/Http/Controllers/UserController.php
And update the code as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | <?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::latest()->paginate(5); return view('user.index',compact('users')) ->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('user.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', 'email' => 'required', ]); User::create($request->all()); return redirect()->route('user.index') ->with('success','User created successfully.'); } /** * Display the specified resource. * * @param \App\User $user * @return \Illuminate\Http\Response */ public function show(User $user) { return view('user.show',compact('user')); } /** * Show the form for editing the specified resource. * * @param \App\User $user * @return \Illuminate\Http\Response */ public function edit(User $user) { return view('user.edit',compact('user')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\User $user * @return \Illuminate\Http\Response */ public function update(Request $request, User $user) { $request->validate([ 'name' => 'required', 'email' => 'required', ]); $user->update($request->all()); return redirect()->route('users.index') ->with('success','User updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\User $user * @return \Illuminate\Http\Response */ public function destroy(User $user) { $user->delete(); return redirect()->route('user.index') ->with('success','User deleted successfully'); } } |
Now find “app/Models/User.php” and put the below content in User.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', ]; } |
Step 6: Add Blade Files
layout.blade.php
Path – resources/views/users/layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application </title> </head> <body> <div class="container"> @yield('content') </div> </body> </html> |
index.blade.php
Path – resources/views/users/index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | @extends('users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 8 CRUD Example f</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('users.create') }}"> Create New user</a> </div> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Name</th> <th>Email</th> <th width="280px">Action</th> </tr> @foreach ($users as $user) <tr> <td>{{ ++$i }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> <td> <form action="{{ route('users.destroy',$user->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a> <a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> {!! $users->links() !!} @endsection |
create.blade.php
Path – resources/views/users/create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | @extends('users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> 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('users.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>email:</strong> <textarea class="form-control" style="height:150px" name="email" placeholder="Email"></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> @endsection |
edit.blade.php
Path – resources/views/users/edit.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @extends('users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> 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('users.update',$user->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="name" value="{{ $user->name }}" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Email:</strong> <textarea class="form-control" style="height:150px" name="email" placeholder="Email">{{ $user->email }}</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> @endsection |
show.blade.php
Path – resources/views/users/show.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | @extends('users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {{ $user->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Email:</strong> {{ $user->email }} </div> </div> </div> @endsection |
Step 7: Run CRUD application
Now the final step in Laravel 8 tutorial CRUD you need to run the CRUD application using this command
1 | php artisan serve |
Now you can open the below URL on your browser:
http://127.0.0.1:8000/users
That’s it! You have successfully performed the Laravel CRUD operation step by step.
Final Words:
With the help of the above guide, you have learned the CRUD generator Laravel 8. If you have any doubt about this Laravel 8 tutorial CRUD, you can ask me through the comments. If you liked this Laravel 8 CRUD generator example, hit the 5 stars. Share the Laravel 8 tutorial CRUD with your friends to help them get knowledge about the Laravel CRUD operation.
Happy Coding!
Hai i have a issue that the users index cant not be found my welcome page works but when i go to users after the i get this error message