Search code examples
ajaxlaravelpassword-encryption

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.


Solution

  • 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
    }
    ...