Closed andreworks closed 7 years ago
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.
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'); } }
Maybe try using query instead of all?
Route::get('datatable', function () { return Datatables::of(QuizTopic::query())->make(true); });
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:
This is my web.php (file routes)
I got this error:
I configured my connections on .env file,
I set configuration on database.php file
My QuizTopic Model file,
System details