Laravel 9 Fetch Data Using Ajax Tutorial Example - Tuts Make
Laravel 9 Fetch Data Using Ajax Tutorial Example - Tuts Make
Laravel 9 Fetch Data Using Ajax Tutorial Example - Tuts Make
tutsmake.com
5-7 minutes
Laravel 9, 8 fetch data using ajax. In this tutorial, we will show you
how to retrieve data from database using ajax in laravel 9, 8 app.
When you are making an app in Laravel 9, 8 app. Then you have
to show some data on the page or modals without refreshing the
page. At that time, you can display the data by using Jquery and
Ajax in laravel 8 app. That too without refreshing the entire page.
So, this tutorial will guide you how to fetch data using ajax in
laravel 9, 8 app. And as well as how to display it.
1 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
Then, Visit laravel 8 app root directory, find .env file. After that, add
database credential:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
In this step, open web.php file and add the following routes into it,
which is placed inside routes directory:
2 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
use App\Http\Controllers\AjaxController;
After that, open this controller in any text editor and add the
following code into it, which is located inside app/http/controllers
directory:
1 <?php
2 namespace App\Http\Controllers;
3 use Illuminate\Http\Request;
4 use Redirect,Response;
5 use App\Models\User;
7 {
8 public function index()
9 {
10 $data['users'] =
11 User::orderBy('id','desc')->paginate(8);
12 return view('list',$data);
3 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
13 }
15 {
17 $user = User::where($where)->first();
18 return Response::json($user);
19 }
20 }
21
22
23
24
25
26
27
28
29
30
31
32
33
34
4 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
In this step, create one blade view file named list.blade.php file.
So, visit the resources/views directory and create list.blade.php
file.
After that, create one modal for display data on it using ajax. So
add the following code into list.blade.php file:
2 hidden="true">
3 <div class="modal-dialog">
4 <div class="modal-content">
5 <div class="modal-header">
6 <h4 class="modal-title"
id="userShowModal"></h4>
7
</div>
8
<div class="modal-body">
9
<form id="userForm" name="userForm"
10
class="form-horizontal">
11
<input type="hidden"
12 name="user_id" id="user_id">
13 <div class="form-group">
14 <label for="name" class="col-
15 sm-2 control-label">Name</label>
16 <div class="col-sm-12">
5 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
17 <input type="text"
25 <div class="col-sm-12">
26 <input type="email"
27 class="form-control" id="email" name="email"
placeholder="Enter Email" value="" required="">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Now, add the following javascript code into list.blade.php file for
display data on modals using ajax in laravel app:
1 <script>
2 $(document).ready(function () {
6 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
3 $.ajaxSetup({
4 headers: {
5 'X-CSRF-TOKEN': $('meta[name="csrf-
6 token"]').attr('content')
7 }
8 });
15 $('#ajax-modal').modal('show');
16 $('#user_id').val(data.id);
17 $('#name').val(data.name);
18 $('#email').val(data.email);
19 })
20 });
21 });
22 </script>
23
24
7 of 8 10/5/2022, 8:29 PM
Laravel 9 Fetch Data using Ajax Tutorial Example - Tuts Make about:reader?url=https%3A%2F%2Fwww.tutsmake.com%2Flaravel-8-f...
http://127.0.0.1:8000/list
Conclusion
Laravel 9, 8 fetch data using ajax tutorial, you have learned how to
retrieve data from database using ajax in laravel 9, 8 app.
8 of 8 10/5/2022, 8:29 PM