Category : #laravel
Tags : #laravel, #laravel crud
In this tutorial, I am going to show you a complete Laravel 9 crud application from scratch. I will show you step by step how to create a complete crud operation in the Laravel 9 application.
We can create crud in Laravel in many processes like we can follow eloquent orm or query builder. In this Laravel 9 crud example tutorial, I am going to use query builder. So from this tutorial, you will learn how to create a complete create read update delete system in Laravel.
In this tutorial, I will follow Laravel 9 crud best practices to show you the good way of coding. I will create a country table for creating this crud application. So let's start PHP Laravel 9 tutorial:
Step 1: Download Fresh Laravel
In this first step, we need a fresh Laravel 9 application. So download it by the below command:
composer create-project laravel/laravel example-app
Step 2: Create Migration
In this step, we need to create a migration for generating a countries
table. So run the below command to create countries
table.
php artisan make:migration create_countries_table
Now update the migration like below:
database/migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('countries');
}
};
Step 3: Connect database
In this step, we will connect the database, and I am going to use MySQL. So connect it like below by updating the .env
file:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=
Now our mysql database is connected with our project. So run php artisan migrate
command to migrate the database.
Step 4: Create Route
Now in this, create a route like the below to complete the crud application in Laravel:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CountryController;
/**
* Country controller route
*/
Route::resource('country',CountryController::class);
Now run php artisan route:list
command to check the available route for which we are going to use for country crud. After running the command you will see the below output.
Read also: Laravel 9 CRUD Tutorial Example Using Eloquent ORM
Step 5: Create Controller
Now in this step, we have to create a country controller to complete all of those methods inside our controller. So create a controller by the below command:
php artisan make:controller CountryController
Now update the controller like below:
app/Http/Controllers/CountryCountroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CountryController extends Controller
{
public function index()
{
return view('welcome',[
'countries' => DB::table('countries')
->orderBy('id','desc')
->get()
]);
}
public function create()
{
return view('create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required'
]);
DB::table('countries')->insert([
'name' => $request->name
]);
return redirect()
->route('country.index')
->with('success','Country creared');
}
public function edit($id)
{
$country = DB::table('countries')->find($id);
return view('edit',['country' => $country]);
}
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required'
]);
DB::table('countries')
->where('id',$id)
->update([
'name' => $request->name
]);
return redirect()
->route('country.index')
->with('success','Country updated');
}
public function show($id)
{
$country = DB::table('countries')->find($id);
return view('show',['country' => $country]);
}
public function destroy($id)
{
DB::table('countries')->where('id',$id)->delete();
return redirect()
->route('country.index')
->with('success','Country deleted');
}
}
Step 6: Create Views
We are almost there. Just we have to create four views files
- welcome.balde.php
- create.blade.php
- edit.blade.php
- show.blade.php
So create these files inside the following path and update them like below:
resources/views/welcome.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 9 crud example</title>
</head>
<body class="antialiased">
<p>{{ session()->get('success') ?: '' }}</p>
<a href="{{ route('country.create') }}">Create New</a>
<table>
<tr>
<th>#</th>
<th>Name</th>
<th>Action</th>
</tr>
@forelse ($countries as $country)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $country->name }}</td>
<td><a href="{{ route('country.edit',$country->id) }}">Edit</a></td>
<td><a href="{{ route('country.show',$country->id) }}">Show</a></td>
<form method="POST" action="{{ route('country.destroy',$country->id) }}">
@csrf
@method('delete')
<button type="submit">Delete</button>
</form>
</tr>
@empty
<p>No country found!</p>
@endforelse
</table>
</body>
</html>
resources/views/edit.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<title>Edit data</title>
</head>
<body class="antialiased">
<form action="{{ route('country.update',$country->id) }}" method="post">
@csrf
@method('patch')
<input type="text" placeholder="name" name="name" value="{{ $country->name }}">
<button type="submit">update</button>
</form>
</body>
</html>
resources/views/show.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<title>Show data</title>
</head>
<body class="antialiased">
<input type="text" value="{{ $country->name }}" readonly>
</body>
</html>
resources/views/create.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Create data</title>
</head>
<body class="antialiased">
<form action="{{ route('country.store') }}" method="post">
@csrf
<input type="text" placeholder="name" name="name">
<button type="submit">submit</button>
</form>
</body>
</html>
Now all are set to go. Now run php artisan serve
command to start the development server and visit the following url to check this Laravel 9 crud application:
VISIT
hope it can help you.