使用 yajra 在 laravel 中使用数据表时没有数据

getting no data when using datatables in laravel with yajra

我正在为用户设置一个数据table,并且有来自 pivot 的权限 但是在跟进文档后我得到一个空白屏幕 这些是我用过的files/methodes

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 和@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>
web.php

Route::get('utilisateurs', 'ComptesController@index')->name('utilisateurs');
Route::get('utilisateurs/getData', 'ComptesController@getData')->name('utilisateurs.getData');

我认为你的js中的url不好:

 url: "{{route('utilisateurs.getData')}}",

看看这个

   // Add this on the top
   use Yajra\DataTables\Html\Builder;
   use DataTables;

   //Controller index method for listing

    public function index(Builder $builder, Request $request)
    {
        $privilege = Privilege::all();

        if (request()->ajax()) {
               return Datatables::of($privilege)
                ->addColumn('libelle_privilege',function(User $user){
                    return $user->privileges()->latest()->first();
                })
                ->make(true);
         }
        $html = $builder->columns([
        ['data' => 'name', 'name' => 'name', 'title' => 'Name','width'=>'10%'],
        ['data' => 'cin', 'name' => 'cin', 'title' => 'Cin','width'=>'10%'],
        ['data' => 'tel', 'name' => 'tel', 'title' => 'Tel','width'=>'10%'],
        ['data' => 'adresse', 'name' => 'adresse', 'title' => 'Address','width'=>'40%'],
        ['data' => 'email', 'name' => 'email', 'title' => 'Email','width'=>'5%'],
        ['data' => 'nom_utilisateur', 'name' => 'nom_utilisateur', 'title' => 'Nom_Utukusateur','width'=>'5%'],
        ['data' => 'libelle_privilege', 'name' => 'libelle_privilege', 'title' => 'Libelle Privilege','width'=>'5%'],
        ])->parameters([
            'order' =>'1',
            'paging'      => true,
            'info'        => false,
            'searchDelay' => 350,
            'dom'         => 'Bfrtip',
            'buttons'     => ['print', 'csv', 'excel', 'pdf'],
            'searching'   => true,
        ]);
        return view('pages.utilisateurs',compact('html'));
    }

    // In Blade file render tables

    <div class="table-responsive">
            {!! $html->table(['class' => 'table table-striped'], true) !!}
    </div>

blade 中完全不需要 js

在 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'},
    ]
});

});

在控制器中添加新列

 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);
}
}

在您看来

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

进行此更改并让我知道状态。