0

i'm using laravel 5.8 there is 2 days when i was doing some search and how to retrieve and show data in datatables but without result

//this is code of a part of my controller file

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Datatables;


use Auth;
use App\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

public function __construct()
{
    $this->middleware('auth');
}

    public function index()
    {

         $user =Datatables::of(User::query())->make(true);

        return view('manage_users.index', ['users' => $user]);
    }

    /**

and the code of a html page and script

<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script
    src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet"
    href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet"
    href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">

<link href="styles/vendor/AdminLTE/css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<link href="bower_components/datatables-responsive/css/dataTables.responsive.css" rel="stylesheet" type="text/css" />

<table id="table" class="table table-bordered table-striped dataTable">
  <thead>
      <tr>
        <th class="th-sm">ID</th>
        <th class="th-sm">Name</th>
        <th class="th-sm">Email</th>
        <th class="th-sm">Type user</th>

      </tr>
    </thead>
    <tbody>



  </tbody>
  <tfoot>
    <tr>
      <th>Id
      </th>
      <th>Name
      </th>
      <th>Email
      </th>
      <th>Type user
      </th>

    </tr>
  </tfoot>
</table>

<script>
         $(document).ready(function() {
    $('#table').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php"
    } );
} );
         </script>

i get empty table whith ajax error like this: DataTables warning: table id=table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

1 Answer 1

1

Your are doing few things wrong.

  1. you did not return your datatables
  2. you have not set the columns on your jquery data tables

Follow this .

On your controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Datatables;
use Auth;
use App\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

public function __construct()
{
    $this->middleware('auth');
}

    public function index()
    {

         $user =User::get();

        return view('manage_users.index', compact('user'));
    }


    public function returnAjaxData()
    {
         $users =User::get();
         return Datatables::of($users)->make(true);
     }
    /**
  • Once you return a datatable set your ajax routes on your route
  • Add a route that leads to returnAjaxData() function with get method
  • After that just use that route in getting data in your Jquery as below
  • Then also mention the columns on your data table jquery as shown below

    <script>
    $(document).ready(function() {
    $('#table').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "url/route/to/returnAjaxData",
         "columns":[
    
        { "data": "id" },
        { "data": "name" },
        { "data": "email" },
        { "data": "type_user" },
    } );
    } );
    </script>
    
  • Remember the columns data are the fields comming from your database.

  • Also remember to change the Ajax url above "url/route/to/returnAjaxData"

Create a route as mentioned earlier that example below:

Route::get('user', array('as' => 'get.user', 'uses' => 'UserController@returnAjaxData'));

Replace the ajax url as "ajax": "/user",

Sign up to request clarification or add additional context in comments.

11 Comments

i get a json response that contains my all data that i need in user route, but my datatables is set in this route manage_users/index it is empty
i get a response that contains my all data that i need in user route that looks like this when server side is true by default, but my datatables is set in this route manage_users/index it is empty. response is like this {"draw":0,"recordsTotal":10,"recordsFiltered":10,"data":[{"id":"23","name":"oussama AMMY DRISS","email":"[email protected]","user_type":"Administrateur","created_at":"2019-04-01 10:49:32","updated_at":"2019-04-09 16:17:47"},{"id":"24","name":"responsable","email":"[email protected]","user_type":"Responsable","created_at":"2019-04-01 ......}],"input":[]}
The most important thing is you need to return datatables. Please understand this. You are returning views and calling it via JQUERY. The above is just for illustration. Also you need to specify column names along with the table rows you have set in your table
If your datatable is set in that route you need to change index route and create a new route that returns datatables. Right now you are returning view with user data
did you change the url in ajax? That leads to returnAjaxData() method
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.