I see you are using
echo json_encode($data);
In Laravel I usually use:
return Response::json($data);
Don't forget to have the use statement:
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
following a guide found on the web I developed a table which show data from a database and a live search bar which filter the results on the table, the problem is that it doesn't show any result (even without typing in the search bar).
I've been trying to solve the problem for hours without success. Can someone help me please?
Many thanks in advance.
This is my view:
@extends('layouts.app')
@section('content')
@push('head')
<script>
$(document).ready(function () {
fetch_customer_data();
function fetch_customer_data(query = '') {
$.ajax({
url: "{{route('formatore.utenti.action')}}",
method: 'GET',
data: {query: query},
dataType: 'json',
success: function (data) {
$('tbody').html(data.table_data);
}
})
}
$(document).on('keyup', '#search', function () {
var query = $(this).val();
fetch_customer_data(query);
});
});
</script>
@endpush
<div class="container" xmlns="http://www.w3.org/1999/html">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Lista utenti</div>
<div class="card-body">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control"
placeholder="Cerca utenti" />
</div>
<div class="table-responsive">
<table class="table table-striped table-hover table-sm">
<thead>
<tr>
<th>Nome</th>
<th>Cognome</th>
<th>Settore</th>
<th>Località</th>
<th>Data di nascita</th>
<th>Telefono</th>
<th>Cellulare</th>
<th>Email</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<a href="{{ route('formatore.dashboard') }}">
<button type="button" class="btn btn-secondary btn-lg btn-block">Torna alla home</button>
</a>
</div>
</div>
</div>
</div>
</div>
@endsection
This is the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UtentiController extends Controller
{
public function __construct()
{
$this->middleware('auth:formatore');
}
public function index()
{
return view('utenti');
}
function action(Request $request)
{
if ($request->ajax()) {
$output = '';
$query = $request->get('query');
if ($query != '') {
$data = DB::table('users')
->select('nome','cognome','settore','indirizzo_localita','data_nascita','telefono','cellulare','email')
->where('nome', 'like', '%' . $query . '%')
->orWhere('cognome', 'like', '%' . $query . '%')
->orWhere('settore', 'like', '%' . $query . '%')
->orWhere('indirizzo_localita', 'like', '%' . $query . '%')
->get();
} else {
$data = DB::table('users')
->select('nome','cognome','settore','indirizzo_localita','data_nascita','telefono','cellulare','email')
->orderBy('created_at', 'desc') //creared at non ce nella select
->get();
}
$total_row = $data->count();
if ($total_row > 0) {
foreach ($data as $row) {
$output .= '
<tr>
<td>'. $row->nome.'</td>
<td>'. $row->cognome.'</td>
<td>'. $row->settore.'</td>
<td>'. $row->indirizzo_localita.'</td>
<td>'. $row->data_nascita.'</td>
<td>'. $row->telefono.'</td>
<td>'. $row->cellulare.'</td>
<td>'. $row->email.'</td>
</tr>
';
}
} else {
$output .= '
<tr>
<td align="center" colspan="5">
Nessun dato trovato
</td>
</tr>
';
}
$data = array(
'table_data' => $output
);
echo json_encode($data);
}
}
}
These are the routes: (the last 2 are about the live search page)
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Auth::routes([
'register' => false,
'verify' => false,
'reset' => false
]);
Route::get('/home', 'HomeController@index');
Route::prefix('formatore')->group(function (){
Route::get('/login', 'Auth\FormatoreLoginController@showLoginForm')->name('formatore.login');
Route::post('/login', 'Auth\FormatoreLoginController@login')->name('formatore.login.submit');
Route::get('/', 'FormatoreController@index')->name('formatore.dashboard');
Route::get('/h', 'HController@index')->name('formatore.h');
Route::get('/q', 'QController@index')->name('formatore.q');
Route::get('/utenti', 'UtentiController@index')->name('formatore.utenti');
Route::get('/utenti/action', 'UtentiController@action')->name('formatore.utenti.action');
});
The columns of the "users" table are "id_utente", "id_formatore", "nome", "cognome", "sesso", "indirizzo_titolo", "indirizzo_via", "indirizzo_cap", "indirizzo_localita", "settore", "data_nascita", "stato_civile", "num_avs", "nazionalita", "permesso", "iban", "telefono", "cellulare", "email", "email_verified_at", "password", "attivo", "remember_token", "id_colloquio", "created_at" and "updated_at"
Please or to participate in this conversation.