How to pass parameter to controller using laravel data table? - php

I need to pass id to my controller function via laravel datatable i.e yajra datatable,
i need to recive id in my function to fetch data but its not posting at all.
I have tried many ways but no luck
below is my code,
jquery,
$(function() {
var id = "<?php echo $id;?>";
// alert(id);
var table = $('#data-table').DataTable({
processing: true,
serverSide: true,
searchable:true,
ajax: "{{ route('vendor.getOperationTimes') }}",
// type:"GET",
// data: {
// "_token": $('meta[name="csrf-token"').attr('content'),
// "id": id
// },
columns: [{
data: 'DT_RowIndex',
name: 'DT_RowIndex'
},
{
data: 'name',
name: 'name'
},
{
data: 'day',
name: 'day'
},
{
data: 'start_time',
name: 'start_time'
},
{
data: 'end_time',
name: 'end_time'
},
{
data: 'action',
name: 'action',
orderable: false,
searchable: false
},
]
});
controller function,
public function getOperationTimes(Request $request)
{
if ($request->ajax()) {
$station_id = $request['id'];
// echo $station_id;exit;
$station = new Station();
$getData = $station->getOperationTimes($station_id);
return Datatables::of($getData)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Edit ';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
route,
Route::get('getOperationTimes',[VendorStationController::class, 'getOperationTimes'])->name('getOperationTimes');
please help me someone at the earliest

Route::get('getOperationTimes/{id}')
ajax: "{{ route('vendor.getOperationTimes', ['id' => id]) }}",
You can try above code.

Related

Laravel - Datatable with post method show raw data instead of table

i have an audit trails report to show the data based on range of date, selected by user. So user will select the range in page (audits) and when click submit will redirect to page (audits_view).
I want to show the data in datatables, but in audits_view, the result that i get is only raw data like this
{"draw":0,"recordsTotal":490,"recordsFiltered":490,"data":[{"id":144586,"event":"created","created_at":"2021-05-19 10:28:50.660","auditable_type":"App\\Model\\Loglogin","user":null},{"id":144585,"event":"created","created_at":"2021-05-19
here is the audits_view blade and controller
<table id="user_table" class="table table-bordered data-table">
<thead>
<tr>
<th width="35%">auditable_type</th>
<th width="35%">event</th>
</tr>
</thead>
</table>
<script>
$(document).ready(function(){
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{url('/')}}/report/audits",
type: "POST",
dataType: "json",
'data': function ( d ) {
d._token = "{{ csrf_token() }}";
},
columns: [
{
data: 'auditable_type',
name: 'auditable_type'
},
{
data: 'event',
name: 'event'
},
]
});
});
</script>
here is the controller
public function audits_view(Request $request)
{
$this->authorize('view_audit_trails');
$user = Auth::user();
$date1= $request->input('date1');
$date2= $request->input('date2');
$data = \OwenIt\Auditing\Models\Audit::with('user')
->select(['id', 'event', 'created_at', 'auditable_type'])
->whereDate('created_at','>=',$date1)>whereDate('created_at','<=',$date2)->get();
return DataTables::of($data)
->make(true);
return view('report.audits_view', compact('date1','date2','user'));
Following is just basic idea:
The view function should be like this:
public function audits_view(Request $request) {
$this->authorize('view_audit_trails');
$user = Auth::user();
$date1= $request->input('date1');
$date2= $request->input('date2');
return view('report.audits_view', compact('date1','date2','user'));
}
and for data table you should create new post route and on that route the function should be like this:
public function audits_data(Request $request) {
$date1= $request->input('date1');
$date2= $request->input('date2');
$data = \OwenIt\Auditing\Models\Audit::with('user')
->select(['id', 'event', 'created_at', 'auditable_type'])
->whereDate('created_at','>=',$date1)>whereDate('created_at','<=',$date2);
return DataTables::of($data)
->make(true);
}
the js should be something like this:
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{url('/')}}/report/audits",
type: "POST",
dataType: "json",
data: function ( d ) {
d._token = "{{ csrf_token() }}",
d.date1 = $('#date1').val(),
d.date2 = $('#date2').val(),
},
columns: [
{
data: 'auditable_type',
name: 'auditable_type'
},
{
data: 'event',
name: 'event'
},
]
});

Yajra Datatable relationship showing records in one column

Here's my ID column data structure
ID: [1,2,3,4,5,6,7,8]
Here is my controller's code.
$transaction = ExpenseEntry::with('transaction');
return DataTables::eloquent($transaction)
->editColumn('transaction', function (ExpenseEntry $expense) {
return $expense->transaction->map(function ($transaction) {
return $transaction->id;
});
})->make(true);
and Javascript Function
$(document).ready(function () {
$('#data-table').DataTable({
"ordering": false,
"processing": true,
"serverSide": true,
"ajax": '{!! route('
expense_dts ') !!}',
columns: [{
data: 'id',
name: 'id'
},
{
data: 'expense_type',
name: 'expense_type'
},
{
data: 'amount',
name: 'amount'
},
{
data: 'date',
name: 'date'
},
{
data: 'description',
name: 'description'
},
{
data: 'transaction',
name: 'transaction.id'
}
]
});
});
It is working but the problem is it is showing all the transacation.id in a table like this [1,2,3,4,5,6]
I want to get them like each id in each row with its column.

jquery-uniform radio button not working yajra-laravel-datatable package (laravel 5.6)

I use yajra-laravel-datatable (laravel 5.6).
I have put jquery-uniform radio button in datatable but that is not working.
I use $.uniform.update() but it is not working.
$(".styled_color_checkbox").uniform({
radioClass: 'choice',
wrapperClass: 'border-indigo-600 text-indigo-800'
});
Blade file code
var oTable = $('.finance_payment_datatable').DataTable({
processing: false,
serverSide: true,
ajax: {
url: '{!! route('admin.finance.invoice.datatable') !!}',
data: function (d) {
var customer_id = $(".customer_select option:selected").val();
if (typeof customer_id == 'undefined'){
customer_id = '0';
}
if (customer_id !== '0') {
d.customer_id = customer_id;
}
}
},
columns: [
{data: 'maincheck', sortable: false, orderable: false, searchable: false},
{data: 'invoice_number', name: 'invoice_number', sortable: true},
{data: 'created_at', name: 'created_at', sortable: true},
{data: 'outstanding_price', name: 'outstanding_price', sortable: true},
],
order: [[2, 'desc']],
"pagingType": "full_numbers",
"language": {
"emptyTable": "No invoice to display",
"search": '<span>Filter:</span> _INPUT_',
"lengthMenu": '<span>Show:</span> _MENU_',
},
autoWidth: false,
'fnDrawCallback': function( oSettings ) {
$.uniform.update();
}
});
Controller file code
public function invoiceDatatable(Request $request) {
$raw = Invoice::with('user')->select('invoices.*');
if ($request->has('customer_id')) {
$raw->where('invoices.user_id', $request->get('customer_id'));
}
$raw->where('invoices.outstanding_price', '>=', '1');
$datatable = app('datatables')->of($raw)
->addColumn('maincheck', function ($raw) {
return '<label><input type="radio" name="invoice" class="styled_color_checkbox outstanding_price" data-amount="'.$raw->outstanding_price.'" value="'.$raw->id.'"></label>';
})
->editColumn('created_at', function ($raw) {
return date('Y/m/d', strtotime($raw->created_at));
})
->editColumn('outstanding_price', function ($raw) {
return '$'.$raw->outstanding_price;
});
return $datatable->make(true);
}

How to use two or more DataTables with using yajra(Laravel)

I have 'post' table in my database. In post table, I have 'LanguageID' column.
When I store/save data, It will make two records, first record with language id = 1 and the second record with language id = 2.
Now, I want to show all 'post' data separately.
Controller
public function index(Request $request)
{
$languages = Language::where('IsActive', 1)->get();
if($request->ajax()) {
foreach ($languages as $lang) {
$post = Post::where('LanguageID', $lang->id)->get();
}
return Datatables::of($post)
->addColumn('action', function($post){
return '<a id="edit" class="btn btn-info"
href="post/' . $post->id . '/edit">
<i class="glyphicon glyphicon-edit icon-white"></i>
Edit
</a>
<button type="button" class="btn btn-danger"
onclick="checkDelete('.$post->id.', this);"
data-token="{{ csrf_token() }}">
<i class="glyphicon glyphicon-trash icon-white"></i>
Delete
</button>';
})
->make(true);
}
return view('pages.back-end.lists.post')->with('languages', $languages);
}
This is my view
This is my yajra
$(function(){
$("#data-post").DataTable({
processing: true,
serverSide: true,
ajax:{
url: "{{ url("post") }}",
data:{ _token: "{{csrf_token()}}", languageid: languageid}
},
columns: [
{ data: 'PostDate', name: 'PostDate'},
{ data: 'PostTitle', name: 'PostTitle' },
{ data: 'PostSlug', name: 'PostTitle' },
{ data: 'action', name: 'action'}
]
});
});
You can use yajra datatables filter based upon language id reffer this - https://datatables.yajrabox.com/collection/custom-filter
or
use can do manual ajax call on button indonesia and english by passing language id and then destroy and reintiate datatable like that -
function customFilters(languageid)
{
$('#datatableid').dataTable().fnDestroy();
$('#datatableid').DataTable({
"processing": true,
"serverSide": true,
"ajax":{
"url": url_link,
"dataType": "json",
"type": "POST",
"data":{ _token: "{{csrf_token()}}", languageid: languageid}
},
"columns": [
{ "data": "id" },
{ "data": "PostDate" },
{ "data": "PostTitle" },
{ "data": "PostSlug"},
{ "data": "LanguageID"},
{ "data": "actions" ,orderable: false, searchable: false}
]
});
}
Hope this will help you.
You have to parse the language ID with doing like this in you dataTable.php
public function language($id){
$this->language = $id;
return $this;
}
public function query(Post $model)
{
$query = Post::where('LanguageID',$this->language)->select('posts.*');
return $this->applyScopes($query);
}
then, call the dataTable manually
$('.tabPost').each(function(){
var lang_id = $(this).attr('id');
var oTable = $("#data-post" + lang_id).DataTable({
processing: true,
serverSide: true,
dom: 'Bfrtip',
order: [[0, 'desc']],
buttons: ['create',
'export',
'print',
'reset',
'reload',
'colvis',
],
ajax:{
url: "{{ url("post") }}",
data: function(d) {
d.LanguageID = lang_id
}
},
columns: [
{ data: 'id', name: 'id'},
{ data: 'PostDate', name: 'PostDate'},
{ data: 'PostTitle', name: 'PostTitle' },
{ data: 'PostSlug', name: 'PostTitle' },
{ data: 'action', name: 'action'}
]
});
$('#'+lang_id).on('click', function(e){
oTable.draw();
e.preventDefault();
});
});
and don't forget to set your controller like this
public function index(PostDataTable $dataTable, Request $request)
{
$languages = Language::where('IsActive', 1)->get();
return $dataTable->language($request->input('LanguageID'))->render('pages.back-end.lists.post', compact('languages'));
}

Query String Filtering with Datatables in Laravel 5.4

im using YajraBox for Datatables, it is Laravel extension.
I want to make it work with my Query String Filtering, so idea is tah i need to pass search request to ajax request
This is part of my form imput:
http://127.0.0.1:8000/lots?make%5B%5D=TOYOTA
So result have to be: only lots made by TOYOTA
This is my script for datatables with YajraBox:
<script type="text/javascript">
$(document).ready(function(){
$('#table').DataTable({
bInfo: false,
searching: false,
processing: true,
serverSide: true,
ajax: '{{ url("/data") }}',
columns: [
{ data: 'date', name: 'date' },
{ data: 'bid', name: 'bid' },
{ data: 'auction_name', name: 'auction_name' },
{ data: 'pics_urls', name: 'pics_urls' },
{ data: 'company', name: 'company' },
{ data: 'model_name_en', name: 'model_name_en' },
{ data: 'model_type_en', name: 'model_type_en' },
{ data: 'grade_en', name: 'grade_en' },
{ data: 'mileage_en', name: 'mileage_en' },
{ data: 'model_year_en', name: 'model_year_en' },
{ data: 'color_en', name: 'color_en' },
{ data: 'displacement', name: 'displacement' },
{ data: 'transmission_en', name: 'transmission_en' },
{ data: 'scores_en', name: 'scores_en' },
{ data: 'start_price_en', name: 'start_price_en' },
{ data: 'result_en', name: 'result_en' }
]
});
});
</script>
This is my controllers for view and for ajax:
public function index()
{
return view('lots.browse');
}
public function indexData(LotFilters $filters)
{
$lots = Lot::filter($filters);
return Datatables::eloquent($lots)->make(true);
}
So what i think, i need to pass some howe ?make%5B%5D=TOYOTA to ajax request ajax: '{{ url("/data") }}',, any one know how to do it?
You can do this like:
"{{ url('/data') }}" + "?make="+value // where value contains TOYOTA in it
and get this value in controller like:
Input::get('make');

Categories