Laravel 8 Ajax Post Request Example Tutorial with Validation

Throughout this laravel Ajax post request you will learn how to send a Ajax Post Request in Laravel 8 application. You can send the errors messages or success message from your backend and show in frontend.

Sending ajax request you need to add routes, one method which return your request from backend and a ajax request which your send from your js file. Let’s start Ajax request in laravel 8 php application step by step easy way.

Step 1. Create Model and Migration

First you need to create model and migration using following command.

php artisan make:model Contact -m

After running the above command you get two files one is model and another is migration.

Now open the database\migrations\2020_09_14_120412_create_contacts_table.php file and update the below code on it;


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('contacts', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Next, open the app\Models\Contact.php file and update fillable properties same like below.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
    use HasFactory;

    protected $fillable = [
      'name', 'email', 'mobile_number', 'subject', 'message'

Step 2. Add Routes

Now you need to add two methods in your routes/web.php file. One for showing the form and another store and send request from controller. So, open the routes\web.php file and update the below code on it;


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;

Route::get('contact', [ ContactController::class, 'create' ]);
Route::post('contact', [ ContactController::class, 'store' ]);
Route::get('/', function () {
    return view('welcome');

Step 3. Update Method in Controller

Next, you need to create a new controller using the following command;

php artisan make:controller ContactController

After successfully created controller, Now open the app\Http\Controllers\ContactController.php file and below code on it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Contact;

class ContactController extends Controller
  public function create()

    return view('contact');

  public function store(Request $request)
          'name'          => 'required',
          'email'         => 'required',
          'subject' => 'required',
          'mobile_number' => 'required',
          'message'       => 'required',

        'name' => $request->name,
        'email' => $request->email,
        'subject' => $request->subject,
        'mobile_number' => $request->mobile_number,
        'message' => $request->message,

      return response()->json(['success'=>'Contact form submitted successfully']);

Step 4. Create Blade File

Next, you need to add a blade file contact.blade.php in your resources/views directory. After that open the resources\views\contact.blade.php file and put the below code on it.

<!DOCTYPE html>
    <title>Laravel Ajax Post Request Example</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//" rel="stylesheet">
    <script src = ""></script>
    <meta name="csrf-token" content="{{ csrf_token() }}" />

    <div class="container">
        <h1>Laravel Ajax Post Request Example with <a href="">Coding Driver</a></h1>
          <span class="success" style="color:green; margin-top:10px; margin-bottom: 10px;"></span>
        <form id="contactform">
            <div class="form-group">
                <input type="text" name="name" class="form-control" placeholder="Enter Name">
                <span class="text-danger" id="nameError"></span>

            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="Enter Email">
                <span class="text-danger" id="emailError"></span>

            <div class="form-group">
                <input type="text" name="subject" class="form-control" placeholder="Enter subject">
                <span class="text-danger" id="subjectError"></span>

            <div class="form-group">
                <strong>Mobile Number:</strong>
                <input type="text" name="mobile_number" class="form-control" placeholder="Enter Mobile">
                <span class="text-danger" id="mobileNumberError"></span>
            <div class="form-group">
                <textarea name="message" class="form-control" placeholder="Enter Your Message"></textarea>
                <span class="text-danger" id="messageError"></span>
            <div class="form-group">
                <button type="button" class="btn btn-success save-data" onclick="submitForm()">Save</button>

Step 5. Send Ajax Request

After create the blade file now add the ajax jquery code after the last closing div of your blade file and send the post request to your controller via ajax.

    function submitForm() {
      let name = $("input[name=name]").val();
      let email = $("input[name=email]").val();
      let subject = $("input[name=subject]").val();
      let mobile_number = $("input[name=mobile_number]").val();
      let message = $("textarea[name=message]").val();
      let _token   = $('meta[name="csrf-token"]').attr('content');

        url: "/contact",
          _token: _token
        success:function(response) {
          if(response) {
        error:function (response) {


Learn Also: Laravel AJAX CRUD Example Tutorial from Scratch

Now, the ajax request in laravel is completed. Hope its work for your, if you have any question let me know in comment. Follow us on twitter for more updated.

Leave a Comment