Laravel 9 CRUD Operation Example Tutorial
Teams applying “Laravel 9 CRUD Operation Example Tutorial” in a production project can use this time-management reference to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi friends,
I am going to show you an example of the laravel 9 crud operation example. step by step explains the step-by-step laravel 9 crud operation example. I’m going to show you about laravel 9 crud operation from scratch. this example will help you with information about Laravel 9 CRUD Example – Laravel 9 CRUD Beginners Tutorial.
In this post, we will give you information about Laravel 9 CRUD Example – Laravel 9 CRUD Beginners Tutorial. Here we will give you detail about Laravel 9 CRUD Example | Laravel 9 CRUD Beginners Tutorial And how to use it also give you a demo for it if it is necessary.
In this example, we will create a post-crud operation using laravel 9. we will create a posts table with title and body column using laravel 9 migration, then we will create routes, controller, view, and model files for the post module.
Let's start following step by step example:
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Add Database Configuration
After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your-database-name
DB_USERNAME=your-database-user-name
DB_PASSWORD=your-database-password
Step 3: Add Table using migration
Create Post Model & Migration For Laravel 9 CRUD Application.
Open again your command prompt. And run the following command on it.
php artisan make:migration create_posts_table --create=posts
After that, open create_posts_table.php file inside LaravelCRUD/database/migrations/ directory. And the update the function up() with following code:
<?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('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
php artisan migrate
Step 4: Add Resource Route
We have to need put below student resource route in routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group that
| contains the "web" middleware group. Now create something great!
|
*/
Route::resource('posts', PostController::class);
Step 5: Add Model and Controller
Here below command help to create the controller and model.
php artisan make:controller PostController --resource --model=Post
Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'body'
];
}
PostController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$post = Post::latest()->get();
return view('post.index',compact('post'))
->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('post.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
Post::create($request->all());
return redirect()->route('posts.index')
->with('success','Post created successfully.');
}
/**
* Display the specified resource.
*
* @param \App\Models\Post $Post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('post.show',compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('post.edit',compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $Post
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$post->update($request->all());
return redirect()->route('posts.index')
->with('success','Post updated successfully');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $Post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index')
->with('success','Post deleted successfully');
}
}
Step 6: Add Blade Files
1) layout.blade.php
2) index.blade.php
3) create.blade.php
4) edit.blade.php
5) show.blade.php
So let's just create the following file and put bellow code.
resources/views/post/layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 CRUD Opration - Nicesnippest.com</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
resources/views/post/index.blade.php
@extends('post.layout')
@section('content')
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
<div class="row">
<div class="col-md-12">
<div class="text-center mt-5">
<h2>Laravel 9 CRUD Example - Nicesnippets.com</h2>
</div>
</div>
<div class="col-md-12 text-end mt-4">
<a class="btn btn-primary" href="{{ route('posts.create') }}"> + Create New Post</a>
</div>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
@if ($message = Session::get('success'))
<div class="alert alert-success mt-3">
<span>{{ $message }}</span>
</div>
@endif
<table class="table table-bordered mt-4">
<tr>
<th>No</th>
<th>Name</th>
<th>Body</th>
<th width="180px">Action</th>
</tr>
@foreach ($post as $post)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->body }}</td>
<td>
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-info btn-sm text-white" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
</div>
@endsection
resources/views/post/create.blade.php
@extends('post.layout')
@section('content')
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
<div class="row">
<div class="col-md-12">
<div class="text-start mt-5">
<h2>Add New Post</h2>
</div>
</div>
<div class="col-md-12 text-end mt-4">
<a class="btn btn-primary" href="{{ route('posts.index') }}">< Back</a>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> 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('posts.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
<input type="text" name="title" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<br>
<div class="form-group">
<strong>Body:</strong>
<textarea class="form-control" style="height:150px" name="body" placeholder="Body"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center mt-3">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
@endsection
resources/views/post/edit.blade.php
@extends('post.layout')
@section('content')
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
<div class="row">
<div class="col-md-12">
<div class="text-start mt-5">
<h2>Edit Post</h2>
</div>
</div>
</div>
<div class="col-md-12 text-end mt-4">
<a class="btn btn-primary" href="{{ route('posts.index') }}">< Back</a>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> 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('posts.update',$post->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>Title:</strong>
<input type="text" name="title" value="{{ $post->title }}" class="form-control" placeholder="Title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<br>
<div class="form-group">
<strong>Body:</strong>
<textarea class="form-control" style="height:150px" name="body" placeholder="Body">{{ $post->body }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center mt-4">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div>
@endsection
resources/views/post/show.blade.php
@extends('post.layout')
@section('content')
<div class="row justify-content-center">
<div class="col-lg-8 margin-tb">
<div class="row">
<div class="col-md-12">
<div class="mt-5">
<h2> Show Post</h2>
</div>
</div>
<div class="col-md-12 text-end mt-4">
<a class="btn btn-primary" href="{{ route('posts.index') }}">< Back</a>
</div>
</div>
</div>
</div>
<div class="row justify-content-center mt-5">
<div class="col-lg-8 margin-tb">
<table class="table table-bordered">
<tr>
<th>Title</th>
<th>Body</th>
</tr>
<tr>
<td>{{ $post->title }}</td>
<td>{{ $post->body }}</td>
</tr>
</table>
</div>
</div>
@endsection
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/posts
You will see layout as like bellow:
List Page:
Add Page:
Edit Page:
Show Page:
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial