yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 858 forks source link

Laravel Datatable don´t work, returns Fatal Error Exception #1006

Closed andreworks closed 7 years ago

andreworks commented 7 years ago

Summary of problem or feature request

I trying to use the laravel datatables in my project, but for some reason I get a strange error. When I do a normal ajax request it´s ok. When I make the normal datatable using just the plugin it´s ok.

Code snippet of problem

This is my view:

@extends('layouts.default')

@section('title','Balconistas')

@section('content')
    <div class="table-responsive">
        <table class="table table-striped" id="allposts">
            <thead>
            <tr>
                <th>Id</th>
                <th>Description</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
@stop

@push('scripts')
<script type="text/javascript">
    $(function () {
        $('#allposts').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{url('datatable')}}',
            columns: [
                {data: 'id', name: 'id'},
                {data: 'description', name: 'description'},
            ]
        });
    });
</script>
@endpush

This is my web.php (file routes)

Route::get('datatable', function () {
        return Datatables::of(QuizTopic::all())->make(true);
    });

I got this error:

FatalErrorException in Model.php line 3351: Call to a member function connection() on null

I configured my connections on .env file,

DB_CONNECTION=foo
DB_HOST=99.9.9.98
DB_PORT=3306
DB_DATABASE=foo
DB_USERNAME=root
DB_PASSWORD=root

I set configuration on database.php file

'foo' => [
                'driver'    => 'mysql',
                'host'      => env('DB_HOST'),
                'port'      => env('DB_PORT'),
                'database'  => env('DB_DATABASE'),
                'username'  => env('DB_USERNAME'),
                'password'  => env('DB_PASSWORD'),
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => true,
                'engine'    => NULL,
            ],

My QuizTopic Model file,

namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    class QuizTopic extends Model
    {
        protected $table   = 'quiz_topics';
        protected $guarded = ['id'];
        protected $hidden  = ['created_at', 'updated_at', 'deleted_at'];

        use SoftDeletes;

        public function questions()
        {
            return $this->belongsToMany('App\Models\QuizQuestion', 'rel_topics_questions', 'topic_id', 'question_id');
        }
    }

System details

yajra commented 7 years ago

Maybe try using query instead of all?

Route::get('datatable', function () {
        return Datatables::of(QuizTopic::query())->make(true);
});