Laravel 7 Tutorial for Beginners - Laravel 7 Ajax Request Tutorial

In this tutorial, you will learn Laravel 7 jQuery Ajax post example. This example will help you Laravel 7 Ajax form submit example. Step by step explain jQuery Ajax request Laravel 7. You can understand a concept of jQuery Ajax post Laravel 7 csrf.

Ajax request is a basic requirement of any PHP project, we are always looking for without page refresh data should store in database and it’s possible only by jquery ajax request. Same thing if you need to write ajax form submit in Laravel 7 then i will help you how you can pass data with Ajax request and get on controller.

You have to just do three things to understand how to use ajax request in Laravel 7, so just follow this three step and you will learn how to use ajax request in your Laravel 7 application.

Step 1: Create Routes

First thing is we put two routes in one for displaying view and another for post ajax. So simple add both routes in your route file.


Route::get('ajaxRequest', 'AjaxController@ajaxRequest');
Route::post('ajaxRequest', 'AjaxController@ajaxRequestPost')->name('');

Step 2: Create Controller

Same things as above for routes, here we will add two new method for routes. One will handle view layout and another for post ajax request method, so let’s add bellow:


namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
     * Create a new controller instance.
     * @return void
    public function ajaxRequest()
        return view('ajaxRequest');
     * Create a new controller instance.
     * @return void
    public function ajaxRequestPost(Request $request)
        $input = $request->all();
        return response()->json(['success'=>'Got Simple Ajax Request.']);

Step 3: Create Blade File

Finally we require to create ajaxRequest.blade.php file and here we will write code of jquery ajax and pass laravel token. So blade file is very important in ajax request. So let’s see bellow file:


<!DOCTYPE html>
    <title>Laravel 7 Ajax 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 7 Ajax Request example</h1>
        <form >
            <div class="form-group">
                <input type="text" name="name" class="form-control" placeholder="Name" required="">
            <div class="form-group">
                <input type="password" name="password" class="form-control" placeholder="Password" required="">
            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="Email" required="">
            <div class="form-group">
                <button class="btn btn-success btn-submit">Submit</button>
<script type="text/javascript">
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        var name = $("input[name=name]").val();
        var password = $("input[name=password]").val();
        var email = $("input[name=email]").val();
           url:"{{ route('') }}",
           data:{name:name, password:password, email:email},

Now you can run your example and see.

You will get output like as bellow:


[2019-09-17 03:54:19] local.INFO: array (
  'name' => 'Hardik Savani',
  'password' => '123456',
  'email' => '',

I hope it can help you

#laravel #php #webdev

Laravel 7 Tutorial for Beginners - Laravel 7 Ajax Request Tutorial
136.80 GEEK