我试图在我的项目中使用laravel数据表,但由于某种原因我得到一个奇怪的错误。当我做一个正常的ajax请求就可以了。当我使用插件制作普通的数据表时就可以了。
任何人都可以帮助我?
这是我的观点:
@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
这是我的web.php(文件路由)
Route::get('datatable', function () {
return Datatables::of(QuizTopic::all())->make(true);
});
我收到了这个错误:
FatalErrorException in Model.php line 3351:
Call to a member function connection() on null
---编辑
我在.env文件上配置了我的连接,
DB_CONNECTION=foo
DB_HOST=99.9.9.98
DB_PORT=3306
DB_DATABASE=foo
DB_USERNAME=root
DB_PASSWORD=root
我在database.php文件上设置配置
'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,
],
我的QuizTopic模型文件,
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');
}
}
但错误仍然存在。