我正在使用yajra / laravel-datatables包
我尝试在laravel中使用Datatables来改善响应时间,但是当我尝试将请求发送到视图时,它返回错误get_class()期望参数1为对象,给出字符串
如果我对查询执行DD,它会返回JSON或ARRAY文件(两者都带有2都会给我错误),我将其附加
// This is the code of my query
$datas= Referred::where('user_id','=', $referred)->select('slug_id','created_at')->get()->toJson();
当我执行DD时,它返回以下内容:
"[{"slug_id":44,"created_at":"2020-11-08 19:52:36"},{"slug_id":74,"created_at":"2020-11-08 19:53:43"}]"
那很好,因为它带给我slug_id和created_at字段中的数据以与DataTables一起显示。但是,当然,在数据仓库中可以有序地连接更多按钮,但这是另一回事了。
但是,当我发送这样的请求时:
return Datatables::of($datas)->make(true);
我得到错误get_class()期望参数1为对象,字符串为Laravel
如果我不通过JSON即数组即输出数据,它将在视图中返回以下内容:
{"draw":0,"recordsTotal":2,"recordsFiltered":2,"data":[{"slug_id":44,"created_at":"2020-11-08 19:52:36"},{"slug_id":74,"created_at":"2020-11-08 19:53:43"}],"input":[]}
它仍然很好,因为它是我需要的数据(slug_id和created_at),但是我无法将其添加到dataTable中,因此我尝试使其在视图中工作:
@extends('layouts.app')
@section('content')
<div class="container justify-content-between">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header justify-content-between">Cliente
</div>
<table id="ref" class="table table-bordered data-table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Fecha</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
@push('scripts')
<script>
$(document).ready(function() {
oTable = $('#ref').DataTable({
"processing": true,
"serverSide": true,
"ajax": "{{route('referreds.edit')}}",
"columns": [
{data: 'array[, ].slug_id', name: 'slug_id'},
{data: 'array[, ].created_at', name: 'created_at'},
]
});
});
</script>
@endsection
我认为模型和路由没有问题是理所当然的,因为它们很好地重定向了我。如果他们需要它们,则表明我和我编辑
无需使用dataTables,我就可以在视图中发送信息,并可以通过前端进行修复,但是我想使用该技术来加快速度。
感谢您的帮助。
欢呼