Search code examples
phpmysqllaraveldatatables

Laravel Class 'Datatables' not found error?


I am developping an application where i retrieve data from Mysql database and show it using Laravel and Datatable, i keep getting this error:

"Class 'Datatables' not found"

I tried different versions of Datatables but none worked and i keep getting the same error.

Here is code:

?php

namespace App\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Student;
use Datatables;

class AjaxdataController extends Controller
{
    function index()
    {
     return view('student.ajaxdata');
     //http://127.0.0:8000/ajaxdata
    }

    function getdata()
    {
     $students = Student::select('id', 'first_name', 'last_name');
     return Datatables::of($students)//error
            ->addColumn('action', function($student){

Solution

  • You need to let php know where this class is located. To do this, add the following use statement to the top of your file:

    use Yajra\DataTables\DataTables;
    

    Now you can use DataTables in your code. Also make sure you use the correct capitalization.


    Update:
    You need to actualy install the package before you can reference it. Take a look at the documentation on how to install this package: https://yajrabox.com/docs/laravel-datatables/master