How To Get Data From Database Using Ajax In Laravel

Hello Artisan,

In this laravel ajax get data from controller example, I will show you step by step that how to use laravel ajax request. You will learn from this tutorial laravel ajax get data from database step by step.

So if you don't know laravel fetch data from database in controller then this example is for you. I will simply use ajax get request to show you how to use ajax get request to show data in laravel.

I will use a modal to show data. So in this how to fetch data from database using model in laravel tutorial, you will completely learn fetching data from database using ajax request. So let's start fetch data from database in laravel using ajax.

 

Step 1: Download Laravel

This is the first step and we need a fresh laravel application; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

 

Step 2: Create Controller

At this point, now we should create a new controller as UserController. In this controller, we will add an index and show a method, that will return users.

Let's update the following code to your controller file:

app/Http/Controllers/UserController.php

namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {        
        $users = User::paginate(20);
 
        return view('users', compact('users'));
    }
 
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = User::find($id);
  
        return response()->json($user);
    }
}

 

Step 3: Add Route

In this step, we need to create a route for listing users. so open your "routes/web.php" file and add the following route.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
/*
|--------------------------------------------------------------------------
| 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::get('users', [UserController::class, 'index']);
Route::get('users/{id}', [UserController::class, 'show'])->name('users.show');

 

Step 4: Create View

In the last step, let's create users.blade.php(resources/views/users.blade.php) for the layout and we will write design code here and put the following code:

resources/views/users.blade.php

 

Read also: Create Events with Ajax in Laravel 9 with Fullcalendar Example

 

Hope this laravel get data from database by id using ajax request tutorial will help you.

 

#laravel #laravel-9x