This tutorial is focused on laravel 9 ajax request example. I’m going to show you how to use ajax in laravel 9. step by step explain laravel 9 jquery ajax post request example. step by step explains laravel 9 ajax form submit example.
Ajax request is a basic requirement of any php project, we are always looking for without page refresh data should store in the database and it's possible only by jquery ajax request. same thing if you need to write an ajax form submit in laravel 9 then I will help you how you can pass data with an ajax request and get on the controller.
In this example, we will create a "posts" table with a title and body column. we will list all posts data and add create button there. when you click on create button we will open the model where you can create a new post using a jquery ajax request. so basically you can fire an ajax post request with a bootstrap model.
so let's follow the below step to do this example.
Step 1: Install Laravel 9
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
<?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'); $table->text('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); }};
Next, run create new migration using laravel migration command as bellow:
php artisan migrate
Now we will create Post model by using following command:
php artisan make:model Post
app/Models/Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model; class Post extends Model{ use HasFactory; /** * Write code on Method * * @return response() */ protected $fillable = [ 'title', 'body' ];}
In this step, we will create a new PostController; in this file, we will add two method index() and store() for render view and create post with json response.
Let's create PostController by following command:
php artisan make:controller PostController
next, let's update the following code to Controller File.
app/Http/Controllers/PostController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use Illuminate\Support\Facades\Validator;use App\Models\Post; class PostController extends Controller{ /** * Write code on Method * * @return response() */ public function index() { $posts = Post::get(); return view('posts', compact('posts')); } /** * Write code on Method * * @return response() */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'title' => 'required', 'body' => 'required', ]); if ($validator->fails()) { return response()->json([ 'error' => $validator->errors()->all() ]); } Post::create([ 'title' => $request->title, 'body' => $request->body, ]); return response()->json(['success' => 'Post created successfully.']); }}
Step 4: Create and Add Routes
Furthermore, open routes/web.php file and add the routes to manage GET and POST requests for render view and ajax post request.
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 which| contains the "web" middleware group. Now create something great!|*/ Route::controller(PostController::class)->group(function(){ Route::get('posts', 'index'); Route::post('posts', 'store')->name('posts.store');});
Step 5: Create Blade File
At last step we need to create posts.blade.php file and in this file we will display all posts and write code for jquery ajax request. So copy bellow and put on that file.