I'm setting up a datatable for users and there privileges from a pivot but im getting a blank screen after following up the documentation these are the files/methodes i used
datatable.js
$(document).ready(function () {
$('#user_table').DataTable({
processing:true,
serverSide:true,
ajax:{
url: "{{route('utilisateurs')}}",
},
columns:[
{data: 'name',name: 'name'},
{data: 'cin',name: 'cin'},
{data: 'tel',name: 'tel'},
{data: 'adresse',name: 'adresse'},
{data: 'email',name: 'email'},
{data: 'nom_utilisateur',name: 'nom_utilisateur'},
{data: 'libelle_privilege',name: 'libelle_privilege'},
]
});
});
ComptesController@getData and @index
public function index(){
$privilege = Privilege::all();
return view('pages.utilisateurs')->with('privileges',$privilege);}
public function getData(Request $request)
{
if ($request->ajax()) {
$users = User::all();
return Datatables::of($users)
->addColumn('libelle_privilege',function(User $user){
return $user->privileges()->latest()->first()->libelle_privilege;
})
->make(true);
}
}
utilisateurs.blade.php table
<table class="table table-striped table-bordered bootstrap-3 dataTable
server-side" id="user_table" role="grid" aria-
describedby="user_table_info">
<thead>
<tr role="row">
<th class="sorting_asc" rowspan="1" colspan="1" aria-
sort="ascending">Nom</th>
<th class="sorting" rowspan="1" colspan="1">CIN</th>
<th class="sorting" rowspan="1" colspan="1">Téléphone</th>
<th class="sorting" rowspan="1" colspan="1">Adresse</th>
<th class="sorting" rowspan="1" colspan="1">Email</th>
<th class="sorting" rowspan="1" colspan="1">Nom d'utilisateur</th>
<th class="sorting" rowspan="1" colspan="1">Privilèges</th>
</tr>
</thead>
<tfoot>
<tr>
<th rowspan="1" colspan="1">Nom</th>
<th rowspan="1" colspan="1">CIN</th>
<th rowspan="1" colspan="1">Téléphone</th>
<th rowspan="1" colspan="1">Adresse</th>
<th rowspan="1" colspan="1">Email</th>
<th rowspan="1" colspan="1">Nom d'utilisateur</th>
<th rowspan="1" colspan="1">Privilèges</th>
</tr>
</tfoot>
</table>
Route::get('utilisateurs', 'ComptesController@index')->name('utilisateurs');
Route::get('utilisateurs/getData', 'ComptesController@getData')->name('utilisateurs.getData');
In jS
var url = "{{ url('/utilisateurs/getData') }}";
$(document).ready(function () {
$('#user_table').DataTable({
processing:true,
serverSide:true,
ajax:{
url: url,
},
columns:[
{data: 'name',name: 'name'},
{data: 'cin',name: 'cin'},
{data: 'tel',name: 'tel'},
{data: 'adresse',name: 'adresse'},
{data: 'email',name: 'email'},
{data: 'nom_utilisateur',name: 'nom_utilisateur'},
{data: 'libelle_privilege',name: 'libelle_privilege'},
]
});
});
and in controller as you are adding new column
public function getData(Request $request)
{
if ($request->ajax()) {
$users = User::all();
return Datatables::of($users)
->addColumn('libelle_privilege',function(User $user){
if ($user->privileges()->exists()) {
$privileges = $user->privileges()->pluck('libelle_privilege')->toArray();
return implode(" ",$privileges);
}
return 'non';
})
->make(true);
}
}
In your view
<table class="table table-striped" id="user_table">
<thead>
<tr role="row">
<th>Name</th>
<th class="sorting" rowspan="1" colspan="1">CIN</th>
<th class="sorting" rowspan="1" colspan="1">Téléphone</th>
<th class="sorting" rowspan="1" colspan="1">Adresse</th>
<th class="sorting" rowspan="1" colspan="1">Email</th>
<th class="sorting" rowspan="1" colspan="1">Nom d'utilisateur</th>
<th class="sorting" rowspan="1" colspan="1">Privilèges</th>
</tr>
</thead>
</table>
make this changes and let me know the status.