Laravel and yajrabox Datatables server side proccessing - php

I'm working on a project which uses Laravel 5.1 as the server side framework, I built a tables to show leads using jQuery Datatables plugin and yajrabox Datatables add-on for Laravel.
I am implementing a server side processing but I need to manipulate the data before send it to the client, for do this I need to first pull all the data i'd like to show (to manipulate it) which make the process really long.
this is the code i'm using:
public function index()
{
return view('leads.test')->with($data);
}
returns the view, and:
public function getLeads()
{
$end = new \MongoDate(Carbon::now()->endOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
$start = new \MongoDate(Carbon::now()->subWeek()->startOfDay()->setTimezone(Client::getTimezone(5))->timestamp);
return \Datatables::of(collect($this->setLeadData(Lead::where('deleted',0)->where('client_id','5')->whereBetween('created_at',[$start,$end])->orderBy('created_at','desc')->get())))->make(true);
}
return the leads, and last one, the manipulation process:
private function setLeadData($leads)
{
$rtn = [];
$row = [];
foreach ($leads as $lead) {
$row['DT_RowId'] = $lead->_id;
if(\Auth::user()->canDeleteLeads()){
$row['checkbox'] = "<input type='checkbox' class='bulk-delete-leads-checkbox' name='bulk_delete_leads[]' id='".$lead->_id."' /><label for='".$lead->_id."'></label>";
}
if(Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->isSameDay(Carbon::now()->startOfDay())){
$row['date'] = "<i class='fa fa-clock-o'></i> at ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->format("H:i");
}else{
$row['date'] = "<i class='fa fa-clock-o'></i> ".Carbon::createFromFormat("Y-m-d H:i:s", $lead->created_at)->timezone(Client::getTimezone())->toDateTimeString();
}
if(is_array($lead->ga_details) && count($lead->ga_details) > 1){
$row['user_type'] = $lead->ga_details['user_type'];
$row['device_category'] = $lead->ga_details['device_category'];
$row['source'] = $lead->ga_details['source'];
$row['campaign'] = $lead->ga_details['campaign'];
$row['ad_group'] = $lead->ga_details['ad_group'];
$row['path'] = $lead->ga_details['path'];
}
$row['last_feed'] = null;
if ($lead->feeds && count($lead->feeds)) {
$row['last_feed'] = array_reverse($lead->feeds)[0]['message'];
}
$row['status'] = $this->setLeadStatusElement($lead->status,$lead->_id);
$row['owner'] = $this->setLeadOwnerElement($lead->owner,$lead->_id);
$data['has_reminder'] = false;
$icon = '';
$reminder = Notification::where('lead_id', $lead->_id)
->where('type', 'lead_reminder')
->where('due_time','<=',Carbon::now()->toDateTimeString())
->where('active',1)
->where('deleted',0)
->first();
if($reminder){
$data['has_reminder'] = true;
$icon = " <i class='fa fa-bell'></i> ";
}
$row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">No Name'.$icon.'</button>';
if(isset($lead->the_lead) && count($lead->the_lead)){
foreach($lead->the_lead as $k => $lead_detail){
if($k == "full_name"){
$row['full_name'] = '<button type="button" class="btn btn-w-m btn-white single-lead-trigger" style="width:100%!important" data-toggle="modal" data-lead-id="' .$lead->_id. '" data-target="#single_lead">' .$lead_detail.$icon.'</button>';
}else{
$row[$k] = $lead_detail;
}
}
if(isset($lead->the_lead['full_name']) && !empty($lead->the_lead['full_name'])){
}
}
$rtn[] = $row;
}
return $rtn;
}
Does any one has any suggestion about how to do it right and proper? thank you very much for any answer! Appreciate it!!

here is my working example, that How I am using this.
My controller method for Datatable
public function getData()
{
$supplier = Supplier::with('manufacturer')->select(['id','proprietor','qualified_person','manufacturer_id','license_no','nth_registration_no','phone','mobile','email','address','status']);
return Datatables::of($supplier)
->editColumn('status', function($supplier){
return (($supplier->status == 1)?"Active":"Deactive");
})
->editColumn('phone', function($supplier){
return "Phone#: ".$supplier->phone." <br /> Mobile#: ".$supplier->mobile;
})
->editColumn('manufacturer_id', function($supplier){
//return $supplier->manufacturer->name;
if($supplier->manufacturer_id != 0){
return $supplier->manufacturer->name;
}else{
return 'Not selected!';
}
})
->addColumn('actions', '
<div class="btn-group">
<i class="fa fa-pencil"></i>
<a href="{!!route("ajax-delete",["type"=>"supplier","id"=>$id ])!!}" data-target="#ajax_delete" data-toggle="modal" class="btn btn-xs btn-danger">
<i class="fa fa-trash-o"></i>
</a>
</div>
')
->remove_column('mobile')
->make(true);
}
my View structure
<table class="table table-bordered table-striped table-condensed flip-content" id="supplier">
<thead class="flip-content">
<tr>
<th>Manufacturer</th>
<th>Qualified Person</th>
<th>Proprietor</th>
<th>License#</th>
<th>Reg#</th>
<th>Contact#</th>
<th>Email</th>
<th>Address</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
</table>
Datatable JS for server side processing
<script type="text/javascript">
var oTable;
$(document).ready(function() {
oTable = $('#supplier').DataTable({
"responsive": true,
"processing": true,
"serverSide": true,
"ajax": "{!!route('supplier-data')!!}",
"columns": [
{data: 'manufacturer_id', name: 'manufacturer_id'},
{data: 'qualified_person', name: 'qualified_person'},
{data: 'proprietor', name: 'proprietor'},
{data: 'license_no', name: 'license_no'},
{data: 'nth_registration_no', name: 'nth_registration_no'},
{data: 'phone', name: 'phone'},
{data: 'email', name: 'email'},
//{data: 'mobile', name: 'mobile'},
{data: 'address', name: 'address'},
{data: 'status', name: 'status'},
{data: 'actions', name: 'actions'},
]
});
});
</script>
here is the example of picking items from multiple laravel relationship.
public function getData()
{
$medicine = Medicine::with(['manufacturer','doseageForm','measureUnit','supplier'])
->select(['id','product_name','generic_name','product_class','manufacturer_id',
'doseage_form_id','measure_unit_id','strenght','status']);
return Datatables::of($medicine)
->editColumn('status', function($medicine){
return (($medicine->status == 1)?"Active":"Deactive");
})
->editColumn('manufacturer_id', function($medicine){
$manufacturer_name = $medicine->manufacturer->name;
return $manufacturer_name;
})
->editColumn('product_name', function($medicine){
return
$medicine->product_name.", ".
$medicine->doseageForm->name.", ".
$medicine->strenght.$medicine->measureUnit->name;
})
->addColumn('supplier',function($medicine){
if($medicine->supplier->count() > 0){
return $medicine->supplier->first()->qualified_person;
}else{
return '---';
}
})
->addColumn('actions', function($medicine){
$edit_route = route('medicine-edit',['id'=>$medicine->id ]);
$del_route = route("ajax-delete",["type"=>"medicine","id"=>$medicine->id ]);
$status = (($medicine->status == 1)?
'<i class="fa fa-eye"></i>'
:
'<i class="fa fa-eye-slash"></i>'
);
$html = '<div class="btn-group">
'.$status.'
<i class="fa fa-pencil"></i>
<a href="'.$del_route.'" data-target="#ajax_delete" alt="delete" data-toggle="modal" class="btn btn-xs btn-danger">
<i class="fa fa-trash-o"></i>
</a>
</div>';
return $html;
})
->make(true);
}

Related

Why does my Yajra Datatables have problem when adding an attribute "selected" in dropdown select?

I have a problem with my Yajra Datable. I have a dropdown yearlist, I tried using for and foreach but it messes up the design of the datatable. when I tried hard coding it it seems to be okay lets say I have a yearlist from 2019 until 2022 but when I remove 2020 it also messes up, when I add selected on one of the options it also messes it up. Here is my code
blade.php
<div class="container-fluid">
<div class="row">
<div class="pull-left mb-2">
<a class="btn btn-info" onClick="add()" href="javascript:void(0)"><i class="fas fa-car-alt"></i> Create
Travel
Order</a>
</div>
<div class="pull-left col-md-2 col-sm-12">
<select class="form-control" id="year" name="selectedYear">
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022" selected>2022</option>
<option value="2023">2023</option>
</select>
</div>
</div>
</div>
<div class="container-fluid py-2" style="overflow-y: auto;height: calc(84vh - 48px); position: relative;top: 0;">
<table class="table table-bordered" id="travel-order">
<thead>
<tr>
<th>Travel Order #</th>
<th>Name</th>
<th>Status</th>
<th>Travel Date</th>
<th></th>
</tr>
</thead>
</table>
</div>
Controller.php
public function travelOrder()
{
return view('pages.travel-order');
}
public function selectYear(Request $request)
{
$data = DB::table('table1')
->where('DateYear', trim($request->year));
if ($request->ajax()) {
return DataTables::of($data)
->addColumn('action', function ($row) {
if ($row->Remarks == "Cancelled") {
$buttonName = "Undo";
$class = "btn-warning";
$fa = "undo-alt";
$onclick = "undo";
$disabled = "disabled";
} else {
$buttonName = "Cancel";
$class = "btn-danger";
$fa = "ban";
$onclick = "cancelled";
$disabled = "";
}
$button =
'<a href="javascript:void(0)" id="' . $row->id . '" data-toggle="tooltip" onClick="editFunc(' . $row->id . ')" data-original-title="Edit" class="edit btn btn-success ' . $disabled . '">
<i class="fas fa-edit"></i>
Edit
</a>
<button type="button" ' . $disabled . ' id="' . $row->id . '" data-toggle="tooltip" onClick="printFunc(' . $row->id . ')" data-original-title="Print" class="print btn btn-primary">
<i class="fas fa-print"></i> Print
</button>';
return $button;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
return view('pages.travel-order');
}
}
Script
//POPULATE DATATABLE
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var TOTable = $('#travel-order').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('page.year.select') }}",
method: "GET",
data: function(d) {
d.year = $('#year').val();
},
},
columns: [{
data: 'TravelOrderno',
name: 'TravelOrderno',
searchable: true
},
{
data: 'Fullname',
name: 'Fullname',
orderable: false,
searchable: true
},
{
data: 'Remarks',
name: 'Remarks',
orderable: false,
searchable: false
},
{
data: 'FromDate',
name: 'FromDate',
searchable: false
},
{
data: 'action',
name: 'action',
orderable: false
},
],
order: [
[1, 'desc']
]
});
//SELECT YEAR
$("#year").on("change", function() {
if ("" != $(this).val()) {
TOTable.draw();
}
});
});
I haven't encountered any error with this, it just that it messes up the datatable design.
below is the image of the messed up design:
This is the one that isn't messed up:
I found the problem but made a temporary solution to it. It was because there isn't any data available for 2022 which is when it is selected first it messes up the design when selecting the previous years. My solution is I first counted my first query if it has rows then used if else statement when it has rows used the year selected but if its empty it will go back to the previous year and display the data's of the previous year and it will not messed up the design when selecting previous years or 2022. It might be also on how it draws the table. I tried using reload but its just the same.

How to remove table filter search from table - ajax with codeigniter 3

So I tried this tutorial : https://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html
and my data shows like this : image of how the data & the features looks
I just want to show the table and using the search feature on the green sign in the picture. But, when I follow the tutorial, there is some features showing (ex : red & blue sign in the picture).
Where I can change to remove the red feature in the picture and move the blue sign feature in the picture to the green sign?
I am still learning at how ajax works. Sorry if my explanation is too complicated. But, I hope you understand... Hope you can help me...
This is the function that I use at my Model - detkelModel.php:
var $table = 'detail_keluarga';
var $column_order = array('nama', 'hubungan', 'personilid', 'lahir', null); //set column field database for datatable orderable
var $column_search = array('nama', 'hubungan', 'personilid', 'lahir'); //set column field database for datatable searchable just nama, hubungan, personilid, lahir are searchable
var $order = array('id' => 'desc'); // default order
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if ($_POST['search']['value']) // if datatable send POST for search
{
if ($i === 0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
} else {
$this->db->or_like($item, $_POST['search']['value']);
}
if (count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if (isset($_POST['order'])) // here order processing
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
} else if (isset($this->order)) {
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if ($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
This is my View of the table and the searchbar - detkel.php:
<div class="navbar navbar-expand navbar-white navbar-light">
<!--left navbar / Data Keluarga label-->
<div class="col-sm-6 float-left">
<h5>Data Keluarga Personil</h5>
</div>
<!-- Right navbar links -->
<ul class="navbar-nav ml-auto">
<!-- Navbar Search, filter, plus data -->
<li class="nav-item">
<div class="form-inline">
<div class="input-group" data-widget="search-bar">
</div>
</div>
</li>
<!-- Filter Dropdown Menu -->
<li>
</li>
<!-- Plus data dropdown menu -->
<li>
<a class="btn btn-default" data-toggle="dropdown" href="#">
</a>
<div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
</div>
</li>
</ul>
</div>
<div class="card">
<div class="table-responsive p-0">
<table id="table" class="table table-hover text-nowraps">
<thead>
<tr>
<th>Nama</th>
<th>id personil</th>
<th>Hubungan</th>
<th>Tanggal Lahir</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
This is the script in my view
<script type="text/javascript">
var save_method; //for save method string
var table;
$(document).ready(function() {
//datatables
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('DetkelController/ajax_list') ?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [{
"targets": [-1], //last column
"orderable": false, //set not orderable
}, ],
});
});
</script>
This is ajax_list() in my Controller DetkelController.php:
public function ajax_list()
{
$list = $this->dk->get_datatables();
$data = array();
//$no = $_POST['start'];
foreach ($list as $dk) {
//$no++;
$row = array();
$row[] = $dk->nama;
$row[] = $dk->hubungan;
$row[] = $dk->personilid;
$row[] = $dk->lahir;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person(' . "'" . $dk->id . "'" . ')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->dk->count_all(),
"recordsFiltered" => $this->dk->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}

How to add Laravel datatable(yajra) to existing project?

Is any possible way, to add yajra datatables to existing project?
If I implement it to blank view.blade.php it works good, but if I put it to my existing project it only show thead.
My controller:
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;
class UserRecordController extends Controller
{
public function __construct()
{
$this->middleware(['role:admin']);
}
public function show()
{
$data = User::all()->all();
return view('management.admin-only.users.users_record')->with('data', $data);
}
public function index(Request $request)
{
if ($request->ajax()) {
$data = User::all()->all();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'View';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('management.admin-only.users.users_record');
}
}
My view:
#extends('layouts.app')
#section('content')
<div class="row admin-panel">
#include('management.common.admin_sidebar')
<div class="col" id="main">
<div class="card panel-stats">
<div class="card-body">
<div class="flash-message">
#foreach (['danger', 'warning', 'success', 'info'] as $msg)
#if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} ×</p>
#endif
#endforeach
</div>
<div class="card-title">
<div class="card-body">
<div class="card-body">
<h2><img class="py-2 mr-3" height="100px" width="auto" src="{{ asset('img/admin/group.jpg') }}">Users</h2>
</div>
Add user
Edit user
Delete user
</div>
<div class="card-body">
<div class="card">
<table class="table table-responsive-md table-striped table-hover data-table" id="users-table">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>E-mail</th>
<th>Add date</th>
<th>Option</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(function () {
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('users_record') }}",
columns: [
{data: 'id', name: 'id'},
{data: 'firstname', name: 'firstname'},
{data: 'surname', name: 'surname'},
{data: 'email', name: 'email'},
{data: 'created_at', name: 'created_at'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
});
function changeUrl(el){
let urlEdit = '{{Route("users_edit_form", ":id" )}}';
urlEdit = urlEdit.replace(':id', el.value);
let urlDelete = '{{Route("users_delete_form", ":id" )}}';
urlDelete = urlDelete.replace(':id', el.value);
document.getElementById('user-edit').href = urlEdit;
document.getElementById('user-delete').href = urlDelete;
}
function checkIfChecked(){
if(document.getElementById('id-selected').checked)
{
console.log(document.getElementById('id-selected').value);
return true
}
else
{
console.log(document.getElementById('id-selected').value);
alert('Choose user to edit/delete!');
return false
}
}
function confirmation(){
let message = confirm("Are you sure?");
if(message)
return true;
else
return false;
}
</script>
#endsection
My routes:
Route::get('/admin_panel/users_record','UserRecordController#index')->name('users_record');
I worked with multiple tutorials - every looked similar. I've just imported all the neccessary webpacks to app.blade.php.
What I've done wrong?
you need two function, one is for load view like
public function index(){
return view('management.admin-only.users.users_record');
}
and another function to get data like
public function get_users_record(Request $request){
if ($request->ajax()) {
$data = User::all()->all();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'View';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
}
route will be
Route::get('/admin_panel/users_record','UserRecordController#index')->name('users_record');
Route::get('/admin_panel/get_users_record','UserRecordController#get_users_record')->name('get_users_record');
and ajax url should be
ajax: "{{ route('get_users_record') }}",

Integration Laravel with database.net - html tags visible in row

I use this component in my project: yajra/laravel-datatables
I have controller:
public function dataTable(Request $request)
{
if ($request->ajax()) {
return Datatables::of($this->model->all())
->addIndexColumn()
->editColumn('enable', function ($row) {
if ($row->enable == 1)
return '<span class="label font-weight-bold label-lg label-light-success label-inline">aktywny</span>';
else return '<span class="label font-weight-bold label-lg label-light-danger label-inline">nieaktywny</span>';
})
->editColumn('name', function ($row) {
return Str::limit($row->name, 80, '...');
})
->addColumn('action', function ($row) {
$btn = '<i class="far fa-edit"></i> ';
$btn .= '<i class="removeItem far fa-trash-alt"></i> ';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
}
and html:
<table class="table table-bordered data-table ">
<thead>
<tr class="resources">
<th>ID</th>
<th>Nazwa produktu</th>
<th>Status</th>
<th width="100px" class="text-center">Akcja</th>
</tr>
</thead>
<tbody class="data-table-center">
</tbody>
</table>
</div>
<div class="datatable datatable-bordered datatable-head-custom" id="kt_datatable"></div>
$(function () {
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('product.dataTable') }}",
language: {
url: "{{ asset('js/lang/Polish.json') }}"
},
iDisplayLength: 50,
render: function (data, type, row) {
return data;
},
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex'},
// {data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'enable', name: 'enable'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
});
In the status (enable) column I see this html instead of the final string. As if a blade would replace such html badly.
My result:
<span class="label font-weight-bold label-lg label-light-success label-inline">aktywny</span>
Prview: https://ibb.co/6tXdH65
How can I fix it?
just add enable in your action in dataTable function
->rawColumns(['action','enable'])
->make(true);

How to pass route with {id} to Datatable blade. To view specific user details

This is my route
Route::get('/view/{id}', [
'uses' => 'ClientController#view',
'as' => 'view'
]);
This is my Client controller
public function view($id){
$client = Client::where('id',$id)->first();
return view('view',compact('client'));
}
This is my datatable
#extends('layouts.datatables_master')
#section('content')
<table class="table table-bordered" id="clients-table">
<thead>
<tr>
<th>ID</th>
<th>Company Name</th>
<th>Name</th>
<th>Surname</th>
<th>ID Number</th>
<th>Created</th>
<th>Last Updated</th>
<th>Actions</th>
</tr>
</thead>
</table>
#stop
#push('scripts')
<script>
$(function() {
$('#clients-table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('business.list') !!}',
columns: [
{ data: 'id', name: 'id' },
{ data: 'companyname', name: 'companyname' },
{ data: 'name', name: 'name' },
{ data: 'surname', name: 'surname' },
{ data: 'idnumber', name: 'idnumber' },
{ data: 'created_at', name: 'created_at' },
{ data: 'updated_at', name: 'updated_at' },
{ data: "actions",
"render": function(data, type, row) {
return '<i class="fa fa-search" title="View"></i> <i class="fa fa-pencil" data-toggle="tooltip" data-placement="top" title="Edit"></i> <i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i>'
;}
}
]
});
});
</script>
#endpush
Problem
My Main issue is passing an ID for all users. If you check my render function on my DataTable I said return route(view,1) where it's 1. I want to pass $id but it says undefined variable. Please help.
You could return the generated route link from your controller like:
return datatables(Client::query())
->addColumn('actions', function ($client) {
return [
'edit_link' => route('view', [$id => $client->id]),
];
})->toJson();
and render in your view as
{
data: "actions",
render: function(data, type, row) {
return
'<a href="' + data.edit_link + '" class="btn btn-xs btn-info">' +
'<i class="fa fa-search" title="View"></i></a>' +
'<a href="#" class="btn btn-xs btn-primary">' +
'<i class="fa fa-pencil" data-toggle="tooltip" data-placement="top" title="Edit"></i>' +
'</a>' +
'<a href="#" class="btn btn-xs btn-danger">' +
'<i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i>' +
'</a>';
}
}
the value of data should be an 'id', and use url() instead of route() :
{ data: "id",
"render": function(data, type, row) {
return '<a href="{{ url("view") }}/"'+ data +'>View</a>';
}
}
Try like this
return '<i class="fa fa-search" title="View"></i>
You pass the client variable to blade
{{ route('view',$client->id); }}

Categories