4

I want to show a list of my posts table rows via datatables jquery plugin with laravel version 5.1 via yajra-laravel-datatables.

For that I do all instructions described in this Quick Start Giude in my project.

this is my Route only for get all data that are Necessary for dataTable:

Route::get('postsData', 'postController@testData');

and this is complete postController php file :

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use yajra\Datatables\Datatables;

class postController extends Controller
{

    public function index ()
    {
        $allPosts = Post::all();
        return \View::make('admin.pages.posts')->with('posts', $allPosts);
    }


    public function create ()
    {
        return \View::make('admin.pages.post_create');
    }


    public function store (Request $request)
    {
        $data = Input::all();

        $rules = array (
            'post_title' => 'required',
            'post_desc'  => 'required'
        );

        $validator = \Validator::make($data, $rules);

        if ($validator->fails()) {
            return \Redirect::to('/admin/posts/create')
                ->withErrors($validator)
                ->withInput();
        } else {

            $post             = new Post();
            $post->post_title = $data['post_title'];
            $post->post_desc  = $data['post_desc'];
            $post->save();

            return \Redirect::to('/admin/posts');
        }
    }


    public function show ($id)
    {
        $post = Post::find($id);

        return \View::make('admin.pages.show_post')->with('post', $post);
    }


    public function edit ($id)
    {
        $post = Post::find($id);
        return \View::make('admin.pages.edit_post')->with('post', $post);
    }


    public function update (Request $request, $id)
    {
        $data = Input::all();

        $rules = array (
            'post_title' => 'required',
            'post_desc'  => 'required'
        );

        $validator = \Validator::make($data, $rules);

        if ($validator->fails()) {
            return \Redirect::to('post/create')
                ->withErrors($validator)
                ->withInput();
        } else {

            $post             = Post::find($id);
            $post->post_title = $data['post_title'];
            $post->post_desc  = $data['post_desc'];
            $post->save();

            return \Redirect::to('admin/posts');
        }
    }


    public function destroy ($id)
    {
        $post = Post::find($id);
        $post->delete();

        return Redirect::to('admin/posts');
    }


    public function postsAll(){
        return View::make('admin.pages.postsAll');
    }

    public function testData(){
        return Datatables::of(Post::select('*'))->make(true);
    }
}

also I add Service Provider and Facade to config/app.php :

return [
    'debug'           => env('APP_DEBUG', false),
    'url'             => 'http://localhost',
    'timezone'        => 'UTC',
    'locale'          => 'en',
    'fallback_locale' => 'en',
    'key'             => env('APP_KEY', 'SomeRandomString'),
    'cipher'          => 'AES-256-CBC',
    'log'             => 'single',


    'providers'       => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Routing\ControllerServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
        Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
        yajra\Datatables\DatatablesServiceProvider::class,
        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],



    'aliases'         => [

        'App'        => Illuminate\Support\Facades\App::class,
        'Artisan'    => Illuminate\Support\Facades\Artisan::class,
        'Auth'       => Illuminate\Support\Facades\Auth::class,
        'Blade'      => Illuminate\Support\Facades\Blade::class,
        'Bus'        => Illuminate\Support\Facades\Bus::class,
        'Cache'      => Illuminate\Support\Facades\Cache::class,
        'Config'     => Illuminate\Support\Facades\Config::class,
        'Cookie'     => Illuminate\Support\Facades\Cookie::class,
        'Crypt'      => Illuminate\Support\Facades\Crypt::class,
        'DB'         => Illuminate\Support\Facades\DB::class,
        'Eloquent'   => Illuminate\Database\Eloquent\Model::class,
        'Event'      => Illuminate\Support\Facades\Event::class,
        'File'       => Illuminate\Support\Facades\File::class,
        'Gate'       => Illuminate\Support\Facades\Gate::class,
        'Hash'       => Illuminate\Support\Facades\Hash::class,
        'Input'      => Illuminate\Support\Facades\Input::class,
        'Inspiring'  => Illuminate\Foundation\Inspiring::class,
        'Lang'       => Illuminate\Support\Facades\Lang::class,
        'Log'        => Illuminate\Support\Facades\Log::class,
        'Mail'       => Illuminate\Support\Facades\Mail::class,
        'Password'   => Illuminate\Support\Facades\Password::class,
        'Queue'      => Illuminate\Support\Facades\Queue::class,
        'Redirect'   => Illuminate\Support\Facades\Redirect::class,
        'Redis'      => Illuminate\Support\Facades\Redis::class,
        'Request'    => Illuminate\Support\Facades\Request::class,
        'Response'   => Illuminate\Support\Facades\Response::class,
        'Route'      => Illuminate\Support\Facades\Route::class,
        'Schema'     => Illuminate\Support\Facades\Schema::class,
        'Session'    => Illuminate\Support\Facades\Session::class,
        'Storage'    => Illuminate\Support\Facades\Storage::class,
        'URL'        => Illuminate\Support\Facades\URL::class,
        'Validator'  => Illuminate\Support\Facades\Validator::class,
        'View'       => Illuminate\Support\Facades\View::class,
        'Datatables' => yajra\Datatables\Datatables::class,

    ],

];

And this is Structure of yajra in vendor composer folder:

enter image description here

But when I open postsData path in browser this error shown:

ReflectionException in Container.php line 737:
Class datatables does not exist

What is Problem?

1
  • I had some problem. I have tried the below solution. No solution work. Commented Jul 14, 2017 at 15:19

5 Answers 5

10

I found the solution after hours of googling and try different ways :

1.first rename your project to a new name.
2.use composer update
3.run php artisan config:cache
4.and run php artisan cache:clear

and try your Code again.

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

2 Comments

Why the new name ?
this post is for 5 months ago, I didn't remember why exactly. maybe for Ensure and Avoid caching.
3

Try change in your controller

Remove:

use yajra\Datatables\Datatables;

Add

use Datatables;

config/app.php

You must have in providers:

 yajra\Datatables\DatatablesServiceProvider::class,

and must have in aliases:

'Datatables' => yajra\Datatables\Datatables::class,

1 Comment

This did it for me.
2

I manage to fix the same bugs.

In config/app.php

Yajra\DataTables\DatatablesServiceProvider::class,
Datatables' => Yajra\DataTables\Facades\Datatables::class,

Just make sure you capitalize 'T' in the '\DataTables\'

Ref. https://github.com/yajra/laravel-datatables/issues/986

Comments

1

same error has also happened if you don't have conf/datatables.php. try installing latest or above 7.x

composer require yajra/laravel-datatables-oracle

add these lines in config/app.php

Yajra\Datatables\DatatablesServiceProvider::class,

'Datatables' => Yajra\Datatables\Facades\Datatables::class,

php artisan config:cache

if you don't have file config/datables.conf. Try to paste these file there. https://gist.github.com/hsali/1cab0d6c81020bf7bce043b65f94373a

Comments

0

Try this:

use Yajra\Datatables\Facades\Datatables;

Comments

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.