In this tutorial you will learn about the Laravel 8 Simple CRUD Application Example Tutorial and its application with practical example.
In this Laravel 8 Simple CRUD Application Example Tutorial, you will learn to create a simple CRUD (Create, Read, Update and Delete) application in Laravel 8. In this Laravel 8 CRUD operation example tutorial I’ll show you how to create a simple crud application in laravel 8. In this example we will learn how to create a simple crud operation application in laravel 8.
Laravel 8 Simple CRUD Application Example Tutorial
In this step by step guide, we will be creating a simple company crud operation application with validation in laravel 8. In this example you will learn how to insert, read, update and delete data from database in laravel 8.
- Download Laravel 8 App
- Setup Database with App
- Create Company Model & Migration For CRUD App
- Create Routes
- Create Company CRUD Controller By Artisan Command
- Create Blade Views File
- Run Laravel CRUD App on Development Server
Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara8blog |
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
.env
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara8blog DB_USERNAME=root DB_PASSWORD= |
Create Model & Migration
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Company -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open create_companies_table.php migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 |
public function up() { Schema::create('companies', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->string('address'); $table->timestamps(); }); } |
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 |
use App\Http\Controllers\CompanyCRUDController; Route::resource('companies', CompanyCRUDController::class); |
Create Company CRUD Controller By Artisan Command
Next, we have to create a controller company crud application. Lets Create a controller named CompanyCRUDController using command given below –
1 |
php artisan make:controller CompanyCRUDController |
Open the CompanyCRUDController.php file and put the following code in it.
app/Http/Controllers/CompanyCRUDController.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 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 |
<?php namespace App\Http\Controllers; use App\Models\Company; use Illuminate\Http\Request; class CompanyCRUDController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data['companies'] = Company::orderBy('id','desc')->paginate(5); return view('companies.index', $data); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('companies.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', 'address' => 'required' ]); $company = new Company; $company->name = $request->name; $company->email = $request->email; $company->address = $request->address; $company->save(); return redirect()->route('companies.index') ->with('success','Company has been created successfully.'); } /** * Display the specified resource. * * @param \App\company $company * @return \Illuminate\Http\Response */ public function show(Company $company) { return view('companies.show',compact('company')); } /** * Show the form for editing the specified resource. * * @param \App\Company $company * @return \Illuminate\Http\Response */ public function edit(Company $company) { return view('companies.edit',compact('company')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\company $company * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $request->validate([ 'name' => 'required', 'email' => 'required', 'address' => 'required', ]); $company = Company::find($id); $company->name = $request->name; $company->email = $request->email; $company->address = $request->address; $company->save(); return redirect()->route('companies.index') ->with('success','Company Has Been updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Company $company * @return \Illuminate\Http\Response */ public function destroy(Company $company) { $company->delete(); return redirect()->route('companies.index') ->with('success','Company has been deleted successfully'); } } |
Create Blade File
Now, create blade file for CRUD Operations accordingly. Let’s directory and some blade view, as following:
- Make Directory Name Companies
- index.blade.php
- create.blade.php
- edit.blade.php
Create directory name companies inside resources/views directory.
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 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Laravel 8 CRUD Tutorial From Scratch</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" > </head> <body> <div class="container mt-2"> <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 8 CRUD Example Tutorial</h2> </div> <div class="pull-right mb-2"> <a class="btn btn-success" href="{{ route('companies.create') }}"> Create Company</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>S.No</th> <th>Company Name</th> <th>Company Email</th> <th>Company Address</th> <th width="280px">Action</th> </tr> @foreach ($companies as $company) <tr> <td>{{ $company->id }}</td> <td>{{ $company->name }}</td> <td>{{ $company->email }}</td> <td>{{ $company->address }}</td> <td> <form action="{{ route('companies.destroy',$company->id) }}" method="Post"> <a class="btn btn-primary" href="{{ route('companies.edit',$company->id) }}">Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> {!! $companies->links() !!} </body> </html> |
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 49 50 51 52 53 54 55 56 57 58 59 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add Company Form - Laravel 8 CRUD</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" > </head> <body> <div class="container mt-2"> <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left mb-2"> <h2>Add Company</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('companies.index') }}"> Back</a> </div> </div> </div> @if(session('status')) <div class="alert alert-success mb-1 mt-1"> {{ session('status') }} </div> @endif <form action="{{ route('companies.store') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Name:</strong> <input type="text" name="name" class="form-control" placeholder="Company Name"> @error('name') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Email:</strong> <input type="email" name="email" class="form-control" placeholder="Company Email"> @error('email') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Address:</strong> <input type="text" name="address" class="form-control" placeholder="Company Address"> @error('address') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <button type="submit" class="btn btn-primary ml-3">Submit</button> </div> </form> </body> </html> |
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 50 51 52 53 54 55 56 57 58 59 60 61 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit Company Form - Laravel 8 CRUD Tutorial</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" > </head> <body> <div class="container mt-2"> <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Company</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('companies.index') }}" enctype="multipart/form-data"> Back</a> </div> </div> </div> @if(session('status')) <div class="alert alert-success mb-1 mt-1"> {{ session('status') }} </div> @endif <form action="{{ route('companies.update',$company->id) }}" method="POST" enctype="multipart/form-data"> @csrf @method('PUT') <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Name:</strong> <input type="text" name="name" value="{{ $company->name }}" class="form-control" placeholder="Company name"> @error('name') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Email:</strong> <input type="email" name="email" class="form-control" placeholder="Company Email" value="{{ $company->email }}"> @error('email') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Company Address:</strong> <input type="text" name="address" value="{{ $company->address }}" class="form-control" placeholder="Company Address"> @error('address') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> </div> <button type="submit" class="btn btn-primary ml-3">Submit</button> </div> </form> </div> </body> </html> |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/companies