I made a live search with laravel and ajax so in my table
td
when I tried to pass named route with parameter inside onClick="location.href=''"
in my controller, it does not work. But named route without parameter have no problem. How can I fix this?
My controller:
function action(Request $request)
{
if($request->ajax())
{
$output = '';
if($query != '')
{
$data = DB::table('banners')
->where('id', 'like', '%'.$query.'%')
->orWhere('Caption_Heading', 'like', '%'.$query.'%')
->orWhere('Caption_Description', 'like', '%'.$query.'%')
->orderBy('id', 'desc')
->get();
}
else
{
$data = DB::table('banners')
->orderBy('id', 'desc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$route = route('banners.edit',$banner);
$output .= '
<tr>
<th scope="row"><input type="checkbox" name="ids[]" class="selectbox" value="'.$row->id.'" onchange="change()"></th>
<td onClick="location.href='.$route.'" style="cursor: pointer">'.$row->id.'</td>
<td style="cursor: pointer"><img src="/storage/'.$row->Banner_Image.'" alt="'.$row->Caption_Heading.'" class="img-thumbnail" width="70px" height="100px"></td>
<td style="cursor: pointer">'.$row->Caption_Description.'</td>
<td style="cursor: pointer">'.$row->Caption_Description.'</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
}
My web.php:
Route::prefix('banners')->group(function() {
Route::get('/', 'AdminVisible\BannerController@index')->name('banner');
Route::get('/action', 'AdminVisible\BannerController@action')->name('banner.action');
Route::get('/create', 'AdminVisible\BannerController@create')->name('banners.create');
Route::post('/create', 'AdminVisible\BannerController@store')->name('banners.store');
Route::delete('/delete','AdminVisible\BannerController@delete')->name('banners.delete');
Route::get('{banner}/edit', 'AdminVisible\BannerController@edit')->name('banners.edit');
Route::patch('/{banner}', 'AdminVisible\BannerController@update')->name('banners.update');
});
My js file:
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('banner.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
});
});