Datatable laravel cannot find the data of an object - ajax

I have a problem that I could not solve, in practice I wanted to get back to my ajax call mixed custom data between data obtained from two models, then I did my queries I did a for cycle and I created a custom array to pass to the ajax call on the view, the problem that gives me an error saying that it doesn't find the data-> folder on the button, sorry but I'm a novice I don't even know if I used a right practice in doing that.
``` public function index(Request $request)
{
if($request->ajax())
{
$folder=folder::with('person')->where('rpe','AB')->where('des',1)->get();
foreach($folder as $folder)
{
$person=Person::find($folder->person);
$data[] = [
'name' => $person->name,
'surname' => $person->surname,
'folder' => $folder->p_r,
];
}
return DataTables::of($data)
->addColumn('action', function($data){
$button = '<button type="button" name="handle" id="'.$data->folder.'" class="handle btn btn-primary btn-sm">Handle</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('test');
}```
This is ajax in the view
``` $(document).ready(function(){
$('#user_table').DataTable({
processing: false,
serverSide: true,
ajax: {
url: "{{ route('test.index') }}",
},
columns: [
{
data: 'name',
name: 'name'
},
{
data: 'surname',
name: 'surname'
},
{
data: 'action',
name: 'action',
orderable: false
}]
});});
```
without ``` id = "'. $ data-> p_r.``` works all all fields in the datatable are printed as soon as I put it tells me: *"draw": 1, "recordsTotal": 3, "recordsFiltered": 0, "data": [], "error": "Exception Message: n trying to get property 'folder’ of non-object"}*

Try one of below solution-
In below line-
$button = 'folder.'" class="handle btn btn-primary btn-sm">Handle';
There is no property as 'folder' inside variable $data. Debug or show the value of $data.
Change below line -
$button = 'folder.'" class="handle btn btn-primary btn-sm">Handle';
To
$button = 'folder ?? NULL).'" class="handle btn btn-primary btn-sm">Handle';

Related

Yajra multi search filters

I am trying to assign a custom search box for each column of the table as shown in this example.
https://datatables.yajrabox.com/eloquent/multi-filter-select
But the code given for this was so confusing. I am not getting any idea how to use that code to achieve this.
Here is my code.
CONTROLLER FUNCTION
function leadManage(Request $request){
$lead_id = $request->get('id');
echo $lead_id;
if ($request->ajax() && $request->draw) {
$data = lead::select('leads.id', 'leads.fname', 'leads.lname', 'leads.phone', 'leads.email', 'leads.created_at', 'lead_status_lists.status_name', 'users.fname as user_fname', 'users.lname as user_lname');
$data = $data->join('users', 'leads.assigned_to', '=', 'users.id');
$data = $data->orderBy('leads.id', 'DESC');
$data = $data->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('date', function($row){
$date = date('d M Y', strtotime($row->created_at));
return $date;
})
->addColumn('action', function($row){
$btn = '<i class="fa fa-pencil font-14"></i>
<button class="btn btn-danger btn-sm" data-toggle="tooltip" data-original-title="Delete" onclick="return deletelead_conf('.$row->id.')"><i class="fa fa-trash font-14"></i></button>';
return $btn;
})
->rawColumns(['name','user_name','date','action'])
->make(true);
}
return view('admin.lead_manage');
}
View File script
$(document).ready(function() {
$('.dataTable').DataTable( {
processing: true,
serverSide: true,
ajax: "{{ route('lead.index') }}",
columns: [
{ data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false },
{data: 'name', name: 'name'},
{data: 'phone', name: 'phone'},
{data: 'email', name: 'email'},
{data: 'course_name', name: 'course_name'},
{data: 'user_name', name: 'user_name'},
{data: 'status_name', name: 'status_name'},
{data: 'date', name: 'date'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
});
I tried to add additional script from given url but it make table body empty and no error. Here is script from given url which I tried to add.
initComplete: function () {
this.api().columns().every(function () {
var column = this;
var input = document.createElement("input");
$(input).appendTo($(column.footer()).empty())
.on('change', function () {
column.search($(this).val(), false, false, true).draw();
});
});
}
But I am not getting any idea how to use those controller function (from given url) in my written code.

Display encrypted password on DataTable using Laravel8/ajax

I have in my database table a column password encrypted.
I'm trying to display data on datatable, I'm using the following code:
if (!$.fn.DataTable.isDataTable('.data-table-standard')) {
/**************afficher les données **********************/
var table = $('.data-table-standard').DataTable({
processing: true,
serverSide: true,
ajax:{
url: "{{ route('users.getdata') }}",
},
columns:[
{data:'name',
name: 'name'
},
{data:'email',
name: 'email'
},
{
data:'password',
name: 'password',
type: "password",
"visible": false
},
{data:'filiale',
name: 'filiale'
},
{data:'display_name',
name: 'display_name'
},
{
data: 'action',
name: 'action',
orderable: false
}
]
});
}
Controller :
function getdata(Request $request)
{
if(request()->ajax())
{
if (Auth::user()->hasRole('admin')){
return datatables()->of(User::leftjoin('role_user','users.id','=','role_user.user_id')->leftjoin('roles','roles.id','=','role_user.role_id')->leftjoin('filiales','filiales.id_filiale','=','users.id_filiale')->get())
->addColumn('action', function($data){
$button = '<table><tr><td>';
$button .= '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Modifier</button>';
$button .= '</td><td>';
$button .= ' <label class="switch" >';
$button .= ' <input type="checkbox" id="'.$data->id.'" class="switch selectRow" ';
if ($data->actif == 1) {
$button .= "checked";
}
$button .= '><span class="slider round"></span></label>';
$button .= '</td></tr></table>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
}
return view('Users.users');
}
But I'm getting the following error :
DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter 'password' for row 0, column 2. For more information about this error, please see http://datatables.net/tn/4
How can I display the encrypted password on datatable ? otherwise how can I display the passwords of users on a datatable ?
If you have any idea please help.
For security Laravel hidden the passwords,
Short way to show the passwords, go to User model
// User.php
...
protected $hidden = [
// 'password', // comment this line or remove it
'remember_token',
];
...
Another way and the better then above one,
Add an attribute
//User.php
...
public function getUserPasswordAttribute()
{
return $this->password;
}
...
at Controller#getdata after get() add this ->append('userPassword')
...
return datatables()->of(User::leftjoin('role_user','users.id','=','role_user.user_id')->leftjoin('roles','roles.id','=','role_user.role_id')->leftjoin('filiales','filiales.id_filiale','=','users.id_filiale')->get()->append('userPassword'))
...
At JS DataTable change password to userPassword
...
{
data:'userPassword',
name: 'userPassword',
type: "password",
"visible": false
}
...

Can't get user_id value Laravel Query

I can't call user_id in my view page using datatable, when I change the select('name') into select('user_id') or select('*') it also show error DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7 Whats wrong with my code? thanks in advance
I have a controller contain following code
public function TeamTask(Request $request)
{
if ($request->ajax()) {
$data = DB::table('posts')->select('name')->selectraw('count(user_id) as total')->selectRaw('SUM(status = "Done") as done')->where('div', Auth::user()->div)->groupBy('name')->get();
return Datatables::of($data)
->addColumn('action', function ($row) {
$btn = ' <span class="fas fa-info"></span>';
return $btn;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
}
return view('task.teamTask',);
}
my view
<script type="text/javascript">
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('team.task') }}",
columns: [{
data: 'DT_RowIndex',
name: 'DT_RowIndex',
orderable: false,
searchable: false,
},
{
data: 'name',
name: 'name',
orderable: false,
},
{
data: 'user_id',
name: 'user_id',
orderable: false,
},
{
data: 'total',
name: 'total',
orderable: false,
},
{
data: 'done',
name: 'done'
},
{
data: 'action',
name: 'action'
},
]
});
});
</script>
I can't call user_id in my view page using datatable, when I change the select('name') into select('user_id') or select('*') it also show error DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7 Whats wrong with my code? thanks in advance
Add ->groupBy('user_id') in your query, this should fix your problem
Try this.
public function TeamTask(Request $request)
{
if ($request->ajax()) {
$data = DB::table('posts')->select('name','user_id')->selectraw('count(user_id) as total')->selectRaw('SUM(status = "Done") as done')->where('div', Auth::user()->div)->groupBy('name')->get();
return Datatables::of($data)
->addColumn('action', function ($row) {
$btn = ' <span class="fas fa-info"></span>';
return $btn;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
}
return view('task.teamTask',);
}

How to filter a record depending of the user connected using Laravel?

I have the next table..
The IDOP column is a key that I'm using for connect in my app instead of email... I would like to be able for filter the IDOP of each user... So the user should only be able to see the rows with
of its corresponding IDOP, how could I filter only his IDOP?
this is the function of my datatable
$('#user_contactabilidadasesor').DataTable({
processing: true,
"scrollX": true,
//serverSide: true,
ajax: {
url: "{{ route('contactabilidadasesor.index') }}",
},
columns: [
{
data: 'idop',
name: 'l.idop',
className: 'uniqueClassName'
},
{
data: 'idop_asesor',
name: 'idop_asesor',
searchable: false, render: function ( data, type, row ) {
if (data == null){ return ''; }else{return (row['idop_asesor'] + ' ' + row['ape_asesor'])};
},
className: 'uniqueClassName'
}
],
});
And this is my query
public function index(Request $request)
{
if($request->ajax())
{
$data = DB::table('tbl_lista_contactabilidad as a')
->select('a.id','a.postventaatcs_id')
->leftjoin('tbl_equipo_postventaatcs as h','h.id','=','a.postventaatc_id')
->leftjoin('users as l','l.id','=','h.asesor_id')
->select(array('a.id','l.name as idop_asesor','l.apellido as ape_asesor','l.idop'));
return DataTables::of($data)
->addColumn('action', function($data){
$button = '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Auditar</button>';
//$button .= ' <button type="button" name="edit" id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('contactabilidadasesor');
}
For filtering you have to use ->where('IDOP', auth()->user()->IDOP) (for single user) of ->whereIn('IDOP', [array of filtering idops]) for multiple IDOPs

Attribute tag is not working in front-end side

I am trying to return an attribute tag from controller to front-end side but attribute link and class is not working.
Click here (image)
Controller Side
public function religionlist(){
$view_data = DB::select("SELECT
id,religion
FROM
hrm_religion");
$religion_data = collect($view_data);
return DataTables::of($religion_data)
->addColumn('Link', function ($religion_data) {
return
' <a href="'. url('/religion') . '/' .
Crypt::encrypt($religion_data->id) .
'/edit' .'"' .
'class="btn btn-success btn-sm block btn-flat"><i class="glyphicon glyphicon-edit-sign" id="customer-confrimed"></i> confirm</a>';
})
->editColumn('id', '{{$id}}')
->setRowId('id')
->make(true);
}
Front-end Side
$(document).ready(function() {
var table = $('#list_table').DataTable( {
"processing": true,
"serverSide": true,
"paging": true,
"ajax": "{{URL::to('/')}}/religion_list",
"columns": [
{ "data": "religion" },
{ "data": "Link", name: 'action', orderable: false, searchable: false},
],
"order": [[0, 'asc']]
});
});
Can you replace your return statement with below one?
return '<i class="glyphicon glyphicon-edit-sign" id="customer-confrimed"></i> Confirm'
Please review this below link
https://github.com/yajra/laravel-datatables/issues/1305
just add ->rawColumns(['Link']) in your code.
return DataTables::of($religion_data)
->addColumn('Link', function ($religion_data) {
return
' <a href="'. url('/religion') . '/' .
Crypt::encrypt($religion_data->id) .
'/edit' .'"' .
'class="btn btn-success btn-sm block btn-flat"><i class="glyphicon glyphicon-edit-sign" id="customer-confrimed"></i> confirm</a>';
})
->editColumn('id', '{{$id}}')
->setRowId('id')
->rawColumns(['Link'])
->make(true);

Resources