Hey devs, did you know that Laravel 9 has been released. So in this tutorial, I will share with you how to create a Laravel 9 crud example step by step. So in this laravel 9 tutorial, you will learn how to create Laravel 9 crud application.
You will learn how to create create, read, update and delete data in Laravel 9 application step by step. I will explain step by step so that you can understand better. To create this Laravel 9 crud tutorial, I will use laravel 9 features.
If you want to install Laravel 9 then Laravel 9.x requires a minimum PHP version of 8.0. So first you have to install PHP 8 on your local machine. I will use a User
model to create this Laravel 9 crud example.
Step 1: Install Laravel 9
To create this Laravel 9 crud tutorial, first, we need to download a fresh Laravel application. So download a fresh Laravel application using the following commands.
composer create-project laravel/laravel example-app
Step 2: Create Migration & Model
As this is the example crud tutorial of Laravel 9, I am going to use the default Laravel 9 User model and users table.
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('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
And update User models like:
app\Models\User.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
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Now after configuring the database, run php artisan migrate
command:
Step 3: Create Routes
We can use a resource controller, But in this laravel 9 crud example, i used normal controllers. So update your web.php like:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function () {
Route::get('/users', 'index')->name('user.index');
Route::get('/user/create', 'create')->name('user.create');
Route::post('/user/store', 'store')->name('user.store');
Route::get('/user/{user}', 'show')->name('user.show');
Route::get('/user/{user}/edit', 'edit')->name('user.edit');
Route::put('/user/{user}/update', 'update')->name('user.update');
Route::delete('/user/{user}/delete', 'delete')->name('user.delete');
});
Step 4: Create Controller
Now we have to complete all of our methods to create Laravel 9 crud tutorial from scratch. So update User controller like:
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
public function index(User $user)
{
return Blade::render('welcome', [
'data' => $user->paginate(5)
]
);
}
public function create()
{
return Blade::render('user.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required'
]);
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make('password')
]);
return redirect()->route('user.index')
->with('success','User created successfully.');
}
public function show(User $user)
{
return Blade::render('user.show', [
'user' => $user
]
);
}
public function edit(User $user)
{
return Blade::render('user.edit', [
'user' => $user
]
);
}
public function update(Request $request, User $user)
{
$request->validate([
'name' => 'required',
'email' => 'required'
]);
$user->update([
'name' => $request->name,
'email' => $request->email
]);
return redirect()->route('user.index')
->with('success','User updated successfully.');
}
public function delete(User $user)
{
$user->delete();
return redirect()->route('user.index')
->with('success','User deleted successfully.');
}
}
Step 5: Create View
Almost all are set to go. Just we need to create some blade files to complete our Laravel 9 crud tutorail. So create it and update that like below:
resources/views/master.blade.php
resources/views/welcome.blade.php
resources/views/user/show.blade.php
resources/views/user/edit.blade.php
resources/views/user/create.blade.php
Now all are set to go. Now you can test by running the command php artisan serve
and visit
Read also: Upload Large CSV File using Queue Job Batching in Laravel
Hope it can help you.
#laravel #laravel-9x