Today, we’ll cover the step-by-step process of building a Laravel CRUD (Create, Read, Update, Delete) web application from scratch.
You will learn step-by-step instructions on how to create a straightforward CRUD operation app in Laravel 9 and how to validate, save, and update form data to the database in Laravel CRUD app using bootstrap throughout this tutorial.
The abbreviation CRUD, which stands for create, read, update, and delete, is used in computer programming to refer to the four operations that are thought to be required to develop a persistent storage application.
A straightforward post-crud operation app with validation and image upload will be implemented in this step-by-step Laravel 9 tutorial. You can learn how to insert, read, update, and remove data from the database with this crud program.
Create a crud operation app in Laravel 9 by following these steps:
First, download the Laravel 9 app.
Step 2: Create a database and app
Step three is to create a company model and migrate it for a CRUD app.
Create Company Controller By Artisan Command in Step 4
Step 5: Design Routes
Create the Blade Views File in Step 6
Create Name Companies Directories
index.blade.php
create.blade.php
edit.blade.php
Run the Laravel CRUD application on the development server in step 7
First, download the Laravel 9 app.
Download or install the latest Laravel 9 setup first. So, to install the new Laravel 9 app on your computer, open the terminal and run the following command:
composer create-project --prefer-dist laravel/laravel:^9.0 laravel-9-crud
Step 2 – Setup Database with App
Setup the database with your downloaded/installed Laravel app. So, you need to find the .env file and setup database details as follows:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password
Step 3 – Create Company Model & Migration For CRUD App
Open again your command prompt. And run the following command on it. To create model and migration file for form:
php artisan make:model Company -m
After that, open company migration file inside laravel-9-crud/database/migrations/ directory. And then update the function up() with the following code:
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('address');
$table->timestamps();
});
}
app/Models/Company.php
Then, open again command prompt and run the following command to create tables in the database:
php artisan migrate
Step 4 – Create Company Controller By Artisan Command
Create a controller by using the following command on the command prompt to create a controller file:
php artisan make:controller CompanyController
After that, visit app/Http/controllers and open the CompanyController.php file. And update the following code into it:
paginate(5);
return view('companies.index', compact('companies'));
}
/**
* 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::create($request->post());
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, Company $company)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'address' => 'required',
]);
$company->fill($request->post())->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');
}
}
Step 5 – Create Routes
Then create routes for laravel crud app. So, open the web.php file from the routes directory of laravel CRUD app. And update the following routes into the web.php file:
use App\Http\Controllers\CompanyController;
Route::resource('companies', CompanyController::class);
Step 6 – Create Blade Views File
Create the directory and some blade view, see the following:
- Make Directory Name Companies
- index.blade.php
- create.blade.php
- edit.blade.php
Create directory name companies inside the resources/views directory.
Note that, create index.blade.php, create.blade.php, and edit.blade.php inside the companies directory. And update the following code into the following files:
index.blade.php:
lang="en">
charset="UTF-8">
Laravel 9 CRUD Tutorial Example
rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
class="container mt-2">
class="row">
class="col-lg-12 margin-tb">
class="pull-left">
Laravel 9 CRUD Example Tutorial
class="pull-right mb-2">
class="btn btn-success" href="{{ route('companies.create') }}"> Create Company
@if ($message = Session::get('success'))
class="alert alert-success">
{{ $message }}
@endif
class="table table-bordered">
S.No
Company Name
Company Email
Company Address
width="280px">Action
@foreach ($companies as $company)
{{ $company->id }}
{{ $company->name }}
{{ $company->email }}
{{ $company->address }}
@endforeach
{!! $companies->links() !!}
create.blade.php:
lang="en">
charset="UTF-8">
Add Company Form - Laravel 9 CRUD
rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
class="container mt-2">
class="row">
class="col-lg-12 margin-tb">
class="pull-left mb-2">
Add Company
class="pull-right">
class="btn btn-primary" href="{{ route('companies.index') }}"> Back
@if(session('status'))
class="alert alert-success mb-1 mt-1">
{{ session('status') }}
@endif
edit.blade.php:
lang="en">
charset="UTF-8">
Edit Company Form - Laravel 9 CRUD Tutorial
rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
class="container mt-2">
class="row">
class="col-lg-12 margin-tb">
class="pull-left">
Edit Company
@if(session('status'))
class="alert alert-success mb-1 mt-1">
{{ session('status') }}
@endif
If you submit the add or edit form blank. So the error message will be displayed with the help of the code given below:
@error('name')
class="alert alert-danger mt-1 mb-1">{{ $message }}
@enderror
Step 7 – Run Development Server
In the last step, open a command prompt and run the following command to start the development server:
php artisan serve
Then open your browser and hit the following URL on it:
http://127.0.0.1:8000/companies
