This article will give you example of laravel 9 blog crud example. you can understand the concept of laravel 9 crud example tutorial.
After this you will make your own blog crud operations, this is only example for blog , you can make any CRUD.
In this example, we will create “Blog” table with “title” and “des” columns. we will use Bootstrap 5. we will use resource routes to create a crud (create read update delete) application in laravel.
Let’s follow the below step to create an insert update delete using in laravel app.
Create Page
You can view the full preview here
Step 1: Install Laravel
If you already install Laravel app then you can skip this step, if not then you may go ahead and execute the below command:
composer create-project laravel/laravel blog
Step 2: Database Configuration
In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel. So let’s open .env file and fill all details like as bellow:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=code
DB_USERNAME=root
DB_PASSWORD=
Step 3: Create Migration Table, Model and Controller
using this command three files will created
php artisan make:model Blog -mrc
- Blog migration file
- Blog Model file
- Blog Controller file
After this command you will find one file in following path “database/migrations” and you have to put bellow code in your migration file for create Blog table.
<?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('blogs', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('des');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
};
Now you have to run this migration by following command:
php artisan migrate
Step 4: Create Route
Here, we need to add resource route for Blog crud application. so open your “routes/web.php” file and add following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::resource('blog', BlogController::class);
Step 5: Update Controller
In this step, now we already create BlogController.
So, let’s copy bellow code and put on BlogController.php file.
app/Http/Controllers/BlogController.php
<?php
namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$all = Blog::all();
// dd($all);
return view('admin.blog.list')->with(['all' => $all]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.blog.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required',
'des' => 'required',
],[
'title.required' => 'Title is required',
'des.required' => 'Description is required',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$blog = Blog::create(
[
'title' => $request->title,
'des' => $request->des,
]);
if($blog){
return redirect()->route('blog.index')->with('message', 'New blog Added Successfully!');
}
return redirect()->route('blog.index')->with('message', 'Some thing went wrong!');
}
/**
* Display the specified resource.
*
* @param \App\Models\Blog $pad
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
{
return view('admin.blog.show')->with(['blog' => $blog]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Blog $pad
* @return \Illuminate\Http\Response
*/
public function edit(Blog $blog)
{
return view('admin.blog.edit')->with(['blog' => $blog]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Blog $pad
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Blog $blog)
{
$validator = Validator::make($request->all(), [
'title' => 'required',
'des' => 'required',
],[
'title.required' => 'Title is required',
'des.required' => 'Description is required',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$blog->title = $request->title;
$blog->des = $request->des;
if($blog->save()){
return redirect()->route('blog.index')->with('message', 'Blog Post Updated Successfully!');
}
return redirect()->back()->with('message', 'Some thing went wrong!');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function destroy(Blog $blog)
{
$blog->delete();
return redirect()->back()->with('message', 'Blog deleted successfully!');;
}
}
Ok, so after run bellow command you will find “app/Models/Product.php” and put bellow content in Product.php file:
Step 6: Update Model
In this step, now we already create Blog.php.
So, let’s copy bellow code and put on Blog.php file.
app/Models/Blog.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
use HasFactory;
protected $table = 'blogs';
protected $fillable = ['title' ,'des' ];
}
Step 7: Add Blade Files
In last step. In this step we have to create just blade file.
so we need to create four blade file as
resources/views/admin/blog/create.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Code.Yoblogger.com</title>
</head>
<body>
<div class="container">
<div class="row">
<h1 style="text-align: center;">Laravel 9 CRUD Tutorial Example - <a href="https://code.yoblogger.com/">Code.yoblogger.com</a>
</div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.index') }}"> Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.create') }}">Create Blog</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<form method="POST" action="{{ route('blog.store') }}">
@csrf
<h1>Create Blog</h1>
<div class="row">
<div class="col">
<div class="mb-3">
<label class="form-label">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea class="form-control" rows="3" name="des"></textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
resources/views/admin/blog/edit.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Code.Yoblogger.com</title>
</head>
<body>
<div class="row">
<h1 style="text-align: center;">Laravel 9 CRUD Tutorial Example - <a href="https://code.yoblogger.com/">Code.yoblogger.com</a>
</div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.index') }}"> Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.create') }}">Create Blog</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<form method="POST" action="{{ route('blog.update', $blog->id) }}">
@csrf
@method('PUT')
<h1>Edit Blog</h1>
<div class="row">
<div class="col">
<div class="mb-3">
<label class="form-label">Title</label>
<input type="text" class="form-control" name="title" value="{{ $blog->title }}">
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea class="form-control" rows="3" name="des"> {{ $blog->des }}</textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3">Save</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
resources/views/admin/blog/list.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Code.Yoblogger.com</title>
</head>
<body>
<div class="row">
<h1 style="text-align: center;">Laravel 9 CRUD Tutorial Example - <a href="https://code.yoblogger.com/">Code.yoblogger.com</a>
</div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.index') }}"> Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.create') }}">Create Blog</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h1>Blog List</h1>
<div class="row">
<div class="col">
<table class="table">
<thead>
<tr>
<th class="col">Post Name</th>
<th class="col text-start">Action</th>
</tr>
</thead>
<tbody>
@forelse ($all as $v )
<tr>
<td>{{ $v->title }}</td>
<td>
<a class="btn btn-secondary" href="{{ route('blog.show', $v->id) }}" role="button">View</a>
<a class="btn btn-primary" href="{{ route('blog.edit', $v->id) }}" role="button">Edit</a>
<form method="post" style="float: left; margin-right: 5px;" action="{{route('blog.destroy',$v->id)}}">
@method('delete')
@csrf
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@empty
@endforelse
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
resources/views/admin/blog/show.blade.php
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Code.Yoblogger.com</title>
</head>
<body>
<div class="row">
<h1 style="text-align: center;">Laravel 9 CRUD Tutorial Example - <a href="https://code.yoblogger.com/">Code.yoblogger.com</a>
</h1>
</div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.index') }}"> Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('blog.create') }}">Create Blog</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h1>Show Blog</h1>
<div class="row">
<div class="col">
<div class="mb-3">
<label class="form-label">Title</label>
<input type="text" class="form-control" value="{{ $blog->title }}" readonly>
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea class="form-control" rows="3" readonly> {{ $blog->des }}</textarea>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
open this URL in your Browser
http://localhost:8000/blog