Pass laravel query result to blade via ajax - ajax

I'm wondering how I can pass a laravel query result back to my ajax function on success. I've read something about json encode but I'm getting kind of confused as how I can do so, not really familiar with json that much.
Ajax
$('#userFilter').change(function(){
var selected = $('#userFilter').find(':selected').val();
var token = $('#token').val();
$.ajax({
type: 'POST',
url: '/admin_panel',
data: {
_token : token,
selected : selected,
},
success: function(data){
$('#usersViewBody').html(data);
}
})
});
Controller
function filterUsers(Request $request) {
$selected = $request->selected;
if ($selected == 0) {
$users = DB::table('users')->paginate(15);
} elseif ($selected == 1){
$users = DB::table('users')
->where('status', '=', '0')
->orWhere('status', '=', '1')
->paginate(15);
} else {
$users = DB::table('users')
->where('status', '=', '2')
->orWhere('status', '=', '3')
->paginate(15);
}
return $users;
View
<tbody id="usersViewBody">
#foreach($users as $user)
<tr>
<td><a data-uid="{{$user->id}}" class="openUserPanel" href="#tabUserInfo"> {{$user->name}}</a></td>
<td>{{$user->department}}</td>
<td>{{$user->position}}</td>
#if($user->status == 0)
<td class="userActive">Active</td>
#elseif($user->status == 1)
<td class="userOOO">On Leave</td>
#elseif($user->status == 2)
<td class="userInactive">Retired</td>
#else
<td class="userInactive">Terminated</td>
#endif
#if($user->status == 2 || $user->status == 3)
<td><button class="btn btn-xs btn-default payrollModalTrigger disabled" data-uid="{{$user->id}}">Update Payroll</button></td>
#else
<td><button class="btn btn-xs btn-default payrollModalTrigger" data-uid="{{$user->id}}">Update Payroll</button></td>
#endif
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="text-center">
{{ $users->links() }}
</div>

Instead of returning collection $users from your controller you need to return view back to the ajax, hence change the return of the controller method from
return $users;
to
return view('name of the view', compact('users'));

Blade is used when the page is first loaded (i.e. it's rendered on the server), it will only run again if you reload the page which is in contrast to what you're trying to do with ajax, i.e. interact with the server without a page reload.
What you want to do in your ajax success call is constrcut HTML for the contents of your table body in javascript along with the data you get in the response from the server.
This package has already done all the heavy lifting for you https://github.com/yajra/laravel-datatables, it would, of course, be good to learn how to do it yourself though!

Related

Laravel search not working. this part does not seem to work "->where('forms.Doc_Desc','LIKE','%'.$query.'%') "

I'm trying to make a search box. Merged 5 tables, works properly in my index.
This is my search function
public function search(Request $request) {
$query = $request['query'];
$Rates = DB::table('rates')
->join('forms', 'forms.id' ,'=', 'rates.FormID')
->join('documents', 'documents.FormID', '=', 'rates.formID')
->join('contractors', 'contractors.user_id', '=', 'rates.contractor_id')
->join('countries', 'countries.ID', '=', 'forms.country')->orwhere('documents.Status', '=', '2')->orwhere('documents.Status', '=', '4')->where('forms.Doc_Desc','LIKE','%'.$query.'%')
->get(['rates.Class', 'rates.Rate', 'rates.Rate2','forms.Doc_Desc','documents.FileName', 'documents.Status', 'countries.country', 'contractors.contractor_name'])->groupBy('Doc_Desc')->map(function ($Rate, $key) { return $Rate;});
if (count($Rates) > 0) {
return view('admin.rates',[
'Rates' => $Rates,
]);
}
else {
return back()->withInput()->with('status','No Luck');
}
}
This is my View
#foreach( $Rates as $Rate=>$ID)
<tr>
<td>
#foreach($ID as $IDs)
{{$IDs->contractor_name }}
#break
#endforeach
</td>
<td> {{ $Rate}} </td>
<td>
#foreach($ID as $IDs)
#if ($IDs->Class == 'Metro')
{{$IDs->Rate }} + {{$IDs->Rate2 }}
<small class="text-warning">*</small>
#break
#endif
#endforeach
</td>
<td>
#foreach($ID as $IDs)
#if ($IDs->Class == 'Regional') {{$IDs->Rate }} + {{$IDs->Rate2 }}<small class="text-warning">*</small>
#break
#endif
#endforeach
</td>
<td>
#foreach($ID as $IDs)
#if ($IDs->Class == 'Remote') {{$IDs->Rate }} + {{$IDs->Rate2 }}<small class="text-warning">*</small>
#break
#endif
#endforeach
</td>
<td>
#foreach($ID as $IDs)
#if ($IDs->Class != 'Metro' && $IDs->Class != 'Regional' && $IDs->Class != 'Remote') <span class="badge badge-secondary"> {{$IDs->Class }} </span>
#endif
#endforeach
</td>
I have two different routes created(one for the plain view and another for the search(with different URIs)
it's really messy. im new and hoping someone can help. Thank You!
also added this above my table #if(isset($Rates))
The problem is in the way you are using your where and orwhere clauses:
->orwhere('documents.Status', '=', '2')
->orwhere('documents.Status', '=', '4')
->where('forms.Doc_Desc','LIKE','%'.$query.'%')
If one of those is true it will return you the result without filtering it, so for example if your status is 2 then no matter your $query variable, the result will be returned.
What you need to do is you have to encapsulate the 2 orwhere clauses into one where clause and let the filtering where stand alone or for your example simply use whereIn.
For example:
->where('forms.Doc_Desc','LIKE','%'.$query.'%')
->where(function ($q) {
$q->where('documents.Status', '=', '2')
->orwhere('documents.Status', '=', '4')
})
Or even simpler which will work for your code
->whereIn('documents.Status',[2,4])
->where('forms.Doc_Desc','LIKE','%'.$query.'%')

Real Live Search and Filter Laravel

I'm new on Laravel and try to implement real live search and filter on my project, but it doesn't work at all. I dont understand ajax ver much and just copy paste the code from other website. I tried to understand the code and I think its correct but it doesn't work, so please help. Thanks
Here is my controller
public function search(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = Service::table('service')
->where('keterangan', 'like', '%'.$query.'%')
->orWhere('biaya', 'like', '%'.$query.'%')
->get();
}
else
{
$data = Service::table('service')
->orderBy('kodeService', 'asc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->kodeService.'</td>
<td>'.$row->keterangan.'</td>
<td>'.$row->biayaService.'</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output
);
echo json_encode($data);
}
}
This is the script
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('live_search.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('#table tbody').html(data.table_data);
}
});
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query)
});
});
Route :
Route::resource('service', 'ServiceController');
Route::get('service/search', 'Service#search')->name('live_search.action');
And index.blade
<table class="table table-striped table-hover table-bordered" id="table">
<thead>
<tr>
<th>Kode Service</th>
<th>Keterangan</th>
<th>Biaya</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
#foreach($service as $data)
<tr>
<td><?= $data->kodeService?></td>
<td><?= $data->keterangan ?></td>
<td><?= $data->biayaService?></td>
<td>
<a class="btn btn-sm btn-info" href="{{ route('service.edit', $data['kodeService']) }}"> <i class="oi oi-pencil"></i> Edit</a>
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#myModal"><span class="oi oi-trash"></span> Hapus</button>
</td>
</tr>
#endforeach
</tbody>
</table>
Put your route like this :
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');
After that open the Browser Console panel (Press F12 to open it) and check the Ajax request in Network tab.
Where you can get the specific error if any in the Response tab.
If you need an extra route to your resource route,you should place the new route before the resource route.
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');

Variable becomes null from controller to View, with no apparent reason

I'm stuck with the next topic. Help would be very appreciated.
Want to make a simple call to a view but, it becomes null during the process. Originally I was not making filters, just showing everything (notes:all()). Right now I'm trying to filter by date, ID, or user name, that's what I capture on my left controls before search button (bottom) is clicked. But now with filters applied (notas -sales-), it becomes null...
I post you my code, so you'll be kind and help me to identify why. I suspect about the routes (it's the fisrt time I define them manually).
The routes
Route::get('notas/notasGet/', 'NotasController#notasGet')->name('notas.notasGet');
Route::post('notas/notasPost/', 'NotasController#notasPost')->name('notas.notasPost');
Route::get('notas/create/', 'NotasController#create')->name('notas.create');
Route::get('notas/store/', 'NotasController#store')->name('notas.store');
Route::put('notas/update/{id}', 'NotasController#update')->name('notas.update');
Route::get('notas/{id}/edit', 'NotasController#edit')->name('notas.edit');
Route::delete('notas/destroy/{id}', 'NotasController#destroy')->name('notas.destroy');
Route::post('notas/cajaAbrir/', 'NotasController#cajaAbrir')->name('notas.cajaAbrir');
Route::post('notas/cajaCortar/', 'NotasController#cajaCortar')->name('notas.cajaCortar');
The ones with problem are notasGet, notasPost, which I call first from index
I call them from app.blade.php
<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/gymmgr/public/registroaccesos/destinationSearchGet/">Acceso</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/gymmgr/public/notas/notasGet/">Venta</a>
</li>
At my controller, dd($notas); works fine
public function notasGet()
{
$fechaInicio = null;
$fechaFin = null;
$headData = array('pageTitle' => 'Admin Home - View all destinations');
$currentDate = Carbon::now()->format('Y-m-d');
//$notas = Nota::whereRaw('dtmHoraCargo IS NULL')->get()->first();
$notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get();
//dd($notas);
$users = null;
$users = User::all();
$cajaAbierta = Caja::whereRaw('dtmCorte IS NULL')->get()->first();
//dd($cajaAbierta);
$data = array('notas'=>$notas,'fechaInicio'=>$fechaInicio,'fechaFin'=>$fechaFin, 'users'=>$users, 'caja'=>$cajaAbierta);
return view('notas.index', $data);
}
public function notasPost(Request $request)
{
$strSearch = $request->input('strSearch');
$fechaInicio = $request->input('fechaInicio');
$fechaFin = $request->input('fechaFin');
$strPatron = $request->input('strPatron');
$notas = null;
switch ($strSearch) {
case 0:
$notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get();
break;
case 1:
$notas = Nota::whereRaw("dtmHoraCargo >= ? AND dtmHoraCargo <= ?", array($fechaInicio." 00:00:00", $fechaFin." 23:59:59"))->get();
break;
case 2:
$notas = Nota::find($strPatron);
break;
case 3:
$notas = Nota::whereDate('dtmHoraCargo', '=', Carbon::today()->toDateString())->get();
break;
}
$users = null;
$users = User::all();
$cajaAbierta = Caja::whereRaw('dtmCorte IS NULL')->get()->first();
// dd($cajaAbierta);
$data = array('notas'=>$notas,'fechaInicio'=>$fechaInicio,'fechaFin'=>$fechaFin, 'users'=>$users, 'caja'=>$cajaAbierta);
dd($notas);
/*It echoes the right nota but suddenly and going to the view it's null and the view calling crashes.
Nothing happens in between, so I don't know*/
return view('notas.index', $data);
//return view('registroaccesos.index', ['headData'=>$headData, 'usuarioSearch'=>$usuarioSearch]);
}
At index
<table class="table">
<thead class="thead-light">
<tr>
<th>Folio</th>
<th>Fecha y hora</th>
<th>Total</th>
<th>Aplica a</th>
<th>Saldo</th>
</tr>
</thead>
<tbody>
#foreach($notas as $nota)
<tr>
<td> {{ $nota->idNota }} ></td>
<td> {{ $nota->dtmHoraCargo }} </td>
<td> {{ $nota->dcmTotal }} </td>
<td> {{ empty($nota->idAplicaA)? '' : $nota->aplicaa->strPaterno . ' ' . $nota->aplicaa->strMaterno . ' ' . $nota->aplicaa->strNombre }} </td>
<td> {{ $nota->dcmSaldo }} </td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
it triggers exception (notas is null, why????)
Trying to get property 'idNota' of non-object (View: C:\xampp\htdocs\gymmgr\resources\views\notas\index.blade.php)
In your switch in case 2 you get single object, in another cases you get collection. So, seems exception happen in this case 2.
case 2:
$notas = Nota::find($strPatron); // Will return object, but not the collection.
break;
You can try this:
case 2:
$notas = collect(Nota::find($strPatron));
break;

laravel delete is not working

I am wondering how to put a csrf token in a form so that delete will work?
Here is my code:
Route:
Route::delete('category_delete/{id}',['as'=>'category_delete','uses'=>'CategoryController#destroy']);
index.blade.php
#section('adminContent')
{!! Form::token() !!}
<div class="table-responsive art-content">
<table class="table table-hover table-striped">
<thead>
<th> NAME</th>
<th> ACTIONS</th>
</thead>
<tbody>
#foreach($categoriesView as $category)
<tr>
<td>{!! $category->name!!}</td>
<td>{!! link_to_route('categories.edit', '', array($category->id),array('class' => 'fa fa-pencil fa-fw')) !!}</td>
<td><button type="button" id="delete-button" class="delete-button" data-url = "{!! url('category_delete')."/".$category->id !!}"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="pagination"> {!! $categoriesView->render() !!}</div>
</div>
#stop
CategoryController:
public function destroy($id,Category $category)
{
$category = $category->find ( $id );
$category->delete ();
Session::flash ( 'message', 'The category was successfully deleted!.' );
Session::flash ( 'flash_type', 'alert-success' );
}
If I used ajax, javascript or jquery, how should the code be?
Using jquery I would do something like the following.
Add the line below in the header of your home view
<meta name="csrf-token" content="{{ csrf_token() }}" />
Now in your javascript
function deleteMe(button)
{
// You might want to put the ajaxSetup function where it will always load globally.
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
if(confirm("Are you sure?"))
{
$.get(
'category_delete/'+button.id,
function( response )
{
// callback is called when the delete is done.
console.log('message = '+ response.message);
}
)
}
}
In index.blade.php, make your id more specific.
...
<td><button type="button" id=".$category->id" class="delete-button" data-url = "{!! url('category_delete')."/".$category->id !!}"><i class="fa fa-trash-o" onlick="deleteMe(this);></i></button>
...
In your controller
public function destroy($id,Category $category){
$category = $category->find ( $id );
$category->delete ();
return response()->json(
'message' => 'Deleted',
)
}
Note: No need to add
Form::token in your view
Hope this helps.....
Updated...........
If you were to do it using laravel only, I will suggest you use a link rather than the button you are using.
In index.blade.php, make your id more specific.
...
<td><a href="category_delete/".$category->id class="delete-button" data-url = "{!! url('category_delete')!!}"><i class="fa fa-trash-o"></i></td>
...
In your controller
public function destroy($id,Category $category){
$category = $category->find ( $id );
$category->delete ();
return view('index');//important.
}
If you want your link to look like a button, use css or a css framework like bootstrap
That should be it. hope this helps again.

filtering table results using ajax

I'm working on a php online shop website using codeigniter framework and i want to be able to filter my table results using checkboxes with ajax
view:
<input type="checkbox" name="brand" value="acer">
<input type="checkbox" name="brand" value="lenovo">
<input type="checkbox" name="pret" value="1000">
<table>
<tbody>
<?php foreach ($laptops_toate as $laptops_all) { ?>
<tr>
<td><img src="http://localhost:82/ci/images/emag/<?php echo $laptops_all->file ?>"></td>
<td><p>Laptop <?php echo $laptops_all->brand ?> </p>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Controller:
public function laptops()
{
$filter = array(
'pret' => $this->input->get('pret'),
'brand' =>$this->input->get('brand')
);
$data['laptops_toate'] = $this->emag_model->laptops_toate_grid($filter);
$this->renders('emag/laptops', $data);
}
model:
public function laptops_toate_grid($filter = null){
$this->db->select('*')
->from('laptop_notebook');
// $query = $this->db->get('laptop_notebook')->result();
// return $query;
if($filter['brand']){
$this->db->where('brand', $filter['brand']);
}
if($filter['pret']){
$this->db->where('pret', $filter['pret']);
}
$query = $this->db->get()->result();
return $query;
}
The problem is now at the ajax code, i don't know how to send the data filter to the server in order to receive the success function.
View:
<script>
$("input[checkbox]").change(function(){
$.ajax({
url: route,
dataType: 'json',
success: function(data){
$.each(data, function(index, element) {
$("tbody").empty();
$("tbody").append("<tr><td>"+
"Laptop "+element.brand+""+
"</td></tr>");
});
}
});
Controller:
public function laptops()
{
$filter = array(
'pret' => $this->input->get('pret'),
'brand' =>$this->input->get('brand')
);
echo json_encode($this->emag_model->laptops_toate_grid($filter));
}
Now just do console.log(data); first inside the $.each() to see what your array looks like.

Resources