Display encrypted password on DataTable using Laravel8/ajax - 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
}
...

Related

How to delete image and file in my database and folder

when I want to delete data, the data in the rekap table is deleted but the file_rekap table and image_rekap table and those in the folder also don't want to be deleted
Controller Rekap
public function delete_rekap($id){
$data = rekap::findOrfail($id);
$images=image_rekap::where("rekap_id",$data->id)->get();
foreach($images as $image){
if (File::exists("images_rekap/".$image->image)) {
File::delete("images_rekap/".$image->image);
}
}
$files= file_rekap::where("rekap_id",$data->id)->get();
foreach($files as $file){
if (File::exists("rekap_file/".$file->file)) {
File::delete("rekap_file/".$file->file);
}
}
$data->delete();
return redirect()->route('rekap')->with('success','Data berhasil dihapus');
}
this my button
<a href="#" class="btn btn-danger delete m-1" data-id="{{ $row->id}}" data-customer="{{ $row->customer}}" >DELETE</a>
script
$('.delete').click(function(){
var rekapid = $(this).attr('data-id');
var customer = $(this).attr('data-customer');
swal({
title: "Yakin",
text: "Kamu akan menghapus data dengan nama "+customer+" ",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
window.location = "/delete_rekap/"+rekapid+" "
swal("Data Berhasil Terhapus", {
icon: "success",
});
} else {
swal("Data tidak jadi dihapus");
}
});
});
</script>
what steps should i do to fix it
if you want to delete, try
unlink php

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

Datatable laravel cannot find the data of an object

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';

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