Yajra Datatable relationship showing records in one column - php

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.

Related

How to pass parameter to controller using laravel data table?

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.

Export Image View in Datatable PDF and Excel

I'm working with a Laravel Blade file. I want to export the table to PDF, and it seems to work correctly. However, there is a problem while displaying the image. It only shows the image path, not the image after exporting.
exportOptions: {
stripHtml: false,
Added but not working
$(document).ready(function() {
var buttonCommon = {
extend : 'pdfHtml5',
exportOptions: {
stripHtml: false,
columns: 'th:not(:last-child)',
exportOptions: {
stripHtml: false,
//columns: 'th:not(:nth-child(2))',
format: {
body: function ( data, row, column, node ) {
// Strip $ from salary column to make it numeric
return column === 4 ?
data.replace( /[$,]/g, '' ) :
data;
}
}
}
}
};
//$.noConflict();
$('.datatable').DataTable({
processing: true,
serverSide: true,
//"oSearch": { "bSmart": false, "bRegex": true },
ajax: '{{ route('customers.getdata') }}',
columns: [
{data: 'id', name: 'id'},
{data: 'profilepic', name: 'profilepic',render: function( data, type, full, meta ) {
if(data==0) { return "<img src=\"/customers/default.png" + "\" height=\"50\"/>"; } else { return "<img src=\"/customers/" + data + "\" height=\"50\"/>"; }
}},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'phone', name: 'phone'},
{data: 'address', name: 'address'},
{data: 'created_by', name: 'created_by'},
{data: 'status', name: 'status',render: function( data, type, full, meta ) {
if(data==1) { return "Active"; } else { return "Inactive"; }
}},
{data: 'action', name: 'action', orderable: false, searchable: false}
],
dom: 'Bfrtip',
buttons: [
$.extend( true, {}, buttonCommon, {
extend: 'copyHtml5',
} ),
$.extend( true, {}, buttonCommon, {
extend: 'excelHtml5',
} ),
$.extend( true, {}, buttonCommon, {
extend: 'pdfHtml5',
} )
]
});
});
I need an image view rather than an image path.

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