Autour de Laravel

DataTables warning: table id=cultures_table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Avatar de RachidDieng
RachidDieng

Bonjour mes chers je suis debutant en php laravel , J'utilise des tables de données et lorsque j'essaie d'afficher des données, j'obtiens cette erreur DataTables warning: table id=cultures_table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

merci de m'aide svp bonne reception

Posté il y a 5 mois
Avatar de CinquièmeDimension
CinquièmeDimension

Salut,

A priori datatable c'est assez simple d'utilisation. Peut-tu nous anvoyer ton code pour qu'on comprenne mieux ?

Posté il y a 5 mois
Avatar de RachidDieng
RachidDieng

  <div class="col-sm-6">
          <ol class="breadcrumb float-sm-right">
            <li class="breadcrumb-item"><a href="{{route('admin.index')}}">{{__('Home')}}</a></li>
            <li class="breadcrumb-item active">{{__('Cultures')}}</li>
          </ol>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.container-fluid -->
  </div>
@endsection

@section('content')
<div class="card card-primary card-outline">
    <div class="card-header">
      <h3 class="card-title">{{__('Cultures Table')}}</h3>
      @can('create_culture')
      <a href="{{route('admin.cultures.create')}}" class="btn btn-primary btn-sm float-right">
        <i class="fa fa-plus"></i>  {{__('Create')}} 
      </a>
      @endcan
    </div>
    <!-- /.card-header -->
    <div class="card-body">
      <div class="row">
        <div class="col-12 table-responsive">
          <table id="cultures_table" class="table table-striped table-hover table-bordered"  width="100%">
            <thead>
            <tr>
              <th width="10px">#</th>
              <th>{{__('Name')}}</th>
              <th>{{__('Sample Type')}}</th>
              <th>{{__('Price')}}</th>
              <th width="80px">{{__('Action')}}</th>
            </tr>
            </thead>
            <tbody>
                
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <!-- /.card-body -->
  </div>

parti model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class Culture extends Model
{
    use SoftDeletes;
    use LogsActivity;
    
    public $guarded=[];

    public function getDescriptionForEvent(string $eventName): string
    {
        return "Culture was {$eventName}";
    }

}

contreller

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Culture;
use App\Http\Requests\Admin\CultureRequest;
use DataTables;

class CulturesController extends Controller
{

    /**
     * assign roles
     */
    public function __construct()
    {
        $this->middleware('can:view_culture',     ['only' => ['index', 'show','ajax']]);
        $this->middleware('can:create_culture',   ['only' => ['create', 'store']]);
        $this->middleware('can:edit_culture',     ['only' => ['edit', 'update']]);
        $this->middleware('can:delete_culture',   ['only' => ['destroy']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cultures=Culture::all();
        return view('admin.cultures.index',compact('cultures'));
    }

    /**
    * get cultures datatable
    *
    * @access public
    * @var  @Request $request
    */
    public function ajax(Request $request)
    {
        $model=Culture::query();

        return DataTables::eloquent($model)
        ->editColumn('price',function($culture){
            return formated_price($culture['price']);
        })
        ->addColumn('action',function($culture){
            return view('admin.cultures._action',compact('culture'));
        })
        ->toJson();
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cultures.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CultureRequest $request)
    {
        Culture::create($request->except('_token'));
        session()->flash('success','Culture saved successfully');
        return redirect()->route('admin.cultures.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $culture=Culture::findOrFail($id);
        return view('admin.cultures.edit',compact('culture'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CultureRequest $request, $id)
    {
        $culture=Culture::findOrFail($id);
        $culture->update($request->except('_token','_method'));

        session()->flash('success','Culture updated successfully');
        return redirect()->route('admin.cultures.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $culture=Culture::findOrFail($id);
        $culture->delete();

        session()->flash('success','Culture deleted successfully');
        return redirect()->route('admin.cultures.index');
    }
}


Posté il y a 5 mois
Avatar de CinquièmeDimension
CinquièmeDimension

Il manque la partie JS si je ne m'abuse

Posté il y a 5 mois
Avatar de RachidDieng
RachidDieng

Bonjour mon cher Cinquiéme dimension merci pour l'attention que vous porté a se sujet voici la partie JS

(function($){

    "use strict";
    
    //active
    $('#cultures').addClass('active');

    //cultures datatable
    var table=$('#cultures_table').DataTable( {
      dom: "<'row'<'col-sm-4'l><'col-sm-4'B><'col-sm-4'f>>" +
      "<'row'<'col-sm-12'tr>>" +
      "<'row'<'col-sm-4'i><'col-sm-8'p>>",
      buttons: [
        {
            extend:    'copyHtml5',
            text:      '<i class="fas fa-copy"></i> '+trans("Copy"),
            titleAttr: 'Copy'
        },
        {
            extend:    'excelHtml5',
            text:      '<i class="fas fa-file-excel"></i> '+trans("Excel"),
            titleAttr: 'Excel'
        },
        {
            extend:    'csvHtml5',
            text:      '<i class="fas fa-file-csv"></i> '+trans("CVS"),
            titleAttr: 'CSV'
        },
        {
            extend:    'pdfHtml5',
            text:      '<i class="fas fa-file-pdf"></i> '+trans("PDF"),
            titleAttr: 'PDF'
        },
        {
          extend:    'colvis',
          text:      '<i class="fas fa-eye"></i>',
          titleAttr: 'PDF'
        }
        
      ],
      "processing": true,
      "serverSide": true,
      "bSort" : false,
      "ajax": {
        url: url("admin/get_cultures")
      },
      // orderCellsTop: true,
      fixedHeader: true,
      "columns": [
         {data:"id"},
         {data:"name"},
         {data:"sample_type"},
         {data:"price"},
         {data:"action",searchable:false,orderable:false,sortable:false}//action
      ],
      "language": {
        "sEmptyTable":     trans("No data available in table"),
        "sInfo":           trans("Showing")+" _START_ "+trans("to")+" _END_ "+trans("of")+" _TOTAL_ "+trans("records"),
        "sInfoEmpty":      trans("Showing")+" 0 "+trans("to")+" 0 "+trans("of")+" 0 "+trans("records"),
        "sInfoFiltered":   "("+trans("filtered")+" "+trans("from")+" _MAX_ "+trans("total")+" "+trans("records")+")",
        "sInfoPostFix":    "",
        "sInfoThousands":  ",",
        "sLengthMenu":     trans("Show")+" _MENU_ "+trans("records"),
        "sLoadingRecords": trans("Loading..."),
        "sProcessing":     trans("Processing..."),
        "sSearch":         trans("Search")+":",
        "sZeroRecords":    trans("No matching records found"),
        "oPaginate": {
            "sFirst":    trans("First"),
            "sLast":     trans("Last"),
            "sNext":     trans("Next"),
            "sPrevious": trans("Previous")
        },
      }
   });

    //delete culture
    $(document).on('click','.delete_culture',function(e){
        e.preventDefault();
        var el=$(this);
        swal({
          title: trans("Are you sure to delete culture ?"),
          type: "warning",
          showCancelButton: true,
          confirmButtonClass: "btn-danger",
          confirmButtonText: trans("Delete"),
          cancelButtonText: trans("Cancel"),
          closeOnConfirm: false
        },
        function(){
          $(el).parent().submit();
        });
    });

})(jQuery);


Posté il y a 5 mois
Avatar de bestmomo
bestmomo

Saput,

Les causes peuvent être multiples. La première chose à faire et d'obtenir le détail de l'erreur avec les outils développeur du navigateur.

Posté il y a 5 mois

Vous ne pouvez pas répondre à ce sujet.