Laravel 9 has released on February 8th, 2022. Laravel developers were eagerly waiting since a long time for this release. There are lots of new features are added in this release. In this Laravel 9, we will see all the features in our upcoming posts. Laravel 9 introduced Anonymous Stub Migration. It resolved the issue of migration class name collision. It supports new string functions of PHP 8. Which are amazing for string operations. Few more query builder functions are introduced. It will make more convenient while performing database operations. Today, I will start with a basic CRUD application in Laravel 9.
Prerequisites
For creating a new application in Laravel 9, you will require to have the below configurations.
- PHP >=8.0.2
- Composer
- Apache/Nginx Server
- VS Code Editor (Optional)
- MySQL (version > 5)
Create a CRUD Application in Laravel 9
For installing a Laravel 9 project setup, I will be using composer. Hence, open the terminal and hit the below command.
composer create-project --prefer-dist laravel/laravel blog
This will take a couple of seconds to install Laravel 9 project setup.
How to Create a Dependent Dropdown in Laravel 8 Using Ajax
Create and Configure Database
In the next step, create a database and configure it. I have already created a database named blog.
CREATE DATABASE blog;
After creating the database, open the project in the VS Code editor and configure it.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=
In the next step, we will setup model, and migration.
Create a Model, Migration, and Controller
Open the terminal, and hit the below command. It will create a model and migration file.
php artisan make:model Post -m
In the next step, you will have to specify the schema of the table(s).
Add Schema in Migration File For CRUD Application
In this post, I will create a basic CRUD application in Laravel 9. Hence, for the demo purpose, I will use blog module.
<?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->id();
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
After creating the migration, let’s run it for dumping the schema.
php artisan migrate
After migrating the database schema, let’s add fillable data in model.
Add Mass Assignment in Model
Firstly, navigate to the Post model. Now, add the below snippet.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title',
'description'
];
}
In the next step, we will create a resource controller. The resource controller will contain the available CRUD methods. These methods will be applicable for performing CRUD operations.
How to Use Toastr JS in Laravel 8 For Showing Notification
Create Resource Controller For CRUD Application in Laravel 9
Open the terminal and hit the below command for creating the resource controller.
php artisan make:controller PostController --resource
After creating the controller, let’s add the below code.
<?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()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$post = Post::create([
'title' => $request->title,
'description' => $request->description
]);
if ($post) {
return redirect()->route('posts.index');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post['title'] = $request->title;
$post['description'] = $request->description;
$post->save();
return redirect()->route('posts.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
}
In the next step, create routes respective to above functions.
Add Resource Route in Laravel 9
Open web.php and add the resource route as shown below.
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::resource('posts', PostController::class);
Lastly, create views for form handling and rendering data.
Create Views For CRUD Application in Laravel 9
Create a new folder named posts inside views folder. Now, create the following views-
- master.blade.php
- index.blade.php
- create.blade.php
- show.blade.php
- edit.blade.php
In the master.blade.php file, I will create a master layout containing bootstrap 5 CDN.
<!doctype html>
<html lang="en">
<head>
<title>Laravel 9 CRUD Application @yield('title')</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container py-5">
@yield('content')
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>
In the index.blade.php, we will list out all the posts. There will be action for View, Edit and Delete.
@extends('posts.master')
@section('content')
@section('title') | Posts Listing @endsection
<div class="row">
<div class="col-xl-12 text-end">
<a href="{{ route('posts.create') }}" class="btn btn-primary"> Create Post </a>
</div>
</div>
<div class="card my-3">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th width="20%">Title</th>
<th width="50%">Description</th>
<th width="20%">Action</th>
<tr>
</thead>
<tbody>
@forelse ($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->description }}</td>
<td>
<form action="{{ route('posts.destroy', $post->id) }}" method="post">
@csrf
<a href="{{ route('posts.show', $post->id) }}" title="View" class="btn btn-sm btn-info">
View
</a>
<a href="{{ route('posts.edit', $post->id) }}" title="Edit" class="btn btn-sm btn-success">
Edit </a>
@method('DELETE')
<button type="submit" onclick="return confirm('Are you sure?');" title="Delete" class="btn btn-sm btn-danger"> Delete </button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="4">
<p class="text-danger text-center fw-bold"> No post found! </p>
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
@endsection
In the create.blade.php, there is a form with few inputs. This is used to create post.
@extends('posts.master')
@section('content')
@section('title') | Create Post @endsection
<div class="row">
<div class="col-xl-6 mx-auto">
<form action="{{ route('posts.store') }}" method="post">
@csrf
<div class="card">
<div class="card-header">
<h5 class="card-title"> Create Post </h5>
</div>
<div class="card-body">
<div class="form-group my-3">
<label for="title"> Title </label>
<input type="text" name="title" id="title" class="form-control" placeholder="Title" />
</div>
<div class="form-group my-3">
<label for="description"> Description </label>
<textarea name="description" id="description" class="form-control" placeholder="Description"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success"> Save </button>
</div>
</div>
</div>
</form>
</div>
</div>
@endsection
You can show the post by clicking on the View action in the table. So, for that there is a separate blade file.
@extends('posts.master')
@section('content')
@section('title') | Show Post @endsection
<div class="row">
<div class="col-xl-6 mx-auto">
<div class="card">
<div class="card-header">
<h5 class="card-title"> Show Post </h5>
</div>
<div class="card-body">
<div class="form-group my-3">
<label for="title"> Title </label>
<input type="text" name="title" id="title" readonly disabled class="form-control"
value="{{ $post ? $post->title : '' }}" />
</div>
<div class="form-group my-3">
<label for="description"> Description </label>
<textarea name="description" id="description" readonly disabled class="form-control"
placeholder="Description">{{ $post->description }}</textarea>
</div>
<div class="form-group">
<a href="{{ route('posts.index') }}" class="btn btn-success"> Back </a>
</div>
</div>
</div>
</div>
</div>
@endsection
Lastly, we have edit.blade.php file for updating the post.
@extends('posts.master')
@section('content')
@section('title') | Update Post @endsection
<div class="row">
<div class="col-xl-6 mx-auto">
<form action="{{ route('posts.update', $post->id) }}" method="post">
@csrf
@method('PUT')
<div class="card">
<div class="card-header">
<h5 class="card-title"> Update Post </h5>
</div>
<div class="card-body">
<div class="form-group my-3">
<label for="title"> Title </label>
<input type="text" name="title" id="title" class="form-control" placeholder="Title" value="{{ $post ? $post->title : '' }}" />
</div>
<div class="form-group my-3">
<label for="description"> Description </label>
<textarea name="description" id="description" class="form-control" placeholder="Description">{{ $post ? $post->description : '' }}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success"> Update </button>
</div>
</div>
</div>
</form>
</div>
</div>
@endsection
Conclusion
We created a basic application having CRUD operations. I hope this will help you to understand the use of Controller, Model, Route, etc.
Leave a Reply