Javascript 无法在Laravel中获取数据
我的LaravelJavascript 无法在Laravel中获取数据,javascript,php,laravel,Javascript,Php,Laravel,我的Laravelindex.blade中有这样的代码,用于显示产品包查询列表中的数据: <div class="kt-portlet"> <div class="kt-portlet__body"> <table id='table_product' class="table table-bordered table-striped table-condensed flip-content&
index.blade
中有这样的代码,用于显示产品包查询列表中的数据:
<div class="kt-portlet">
<div class="kt-portlet__body">
<table id='table_product' class="table table-bordered table-striped table-condensed flip-content">
<thead class="flip-content">
<tr>
<th class="no-sort">Action</th>
<th class="">Name</th>
<th class="">Price</th>
@if (Auth::user()->is_super_admin)
<th class="no-sort">Hub</th>
@endif
<th class="">Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
@stop
@section('page_css')
<style type="text/css">
.old_price {
color: red;
text-decoration: line-through;
font-style: italic;
}
</style>
@endsection
@section('add_js')
@parent
<script type="text/javascript">
var table;
$(document).ready(function () {
{!! $form->printJs(); !!}
$('#search_btn').click(function () {
table.ajax.reload(function () {
$('#search_btn').removeClass('kt-spinner kt-spinner--right kt-spinner--sm kt-spinner--light').attr('disabled', false);
$('.no-sort').removeClass('sorting_asc');
});
});
table = $("#table_product").DataTable({
searchDelay: 500,
filter: true,
dom: `<'row'<'col-sm-12'tr>>
<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>`,
responsive: false,
pageLength: 10,
processing: true,
serverSide: true,
ajax: {
"url": "{{ URL::to('/') }}/product-bundle/data",
"type": "POST",
"headers": {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
"data": function (d) {
d.search = $('#global_search').val();
d.status = $('#status').val();
d.hub_id = $('#hub_id').val();
}
},
columns: [{
data: "id"
},
{
data: "name"
},
{
data: "price"
},
@if (Auth::user()->is_super_admin)
{
data: "hub.name"
},
@endif
{
data: "status"
}],
columnDefs: [
{
targets: 0,
orderable: false,
render: function (data, type, row) {
return '<a href="/product-bundle/' + row['id'] + '" class="btn btn-sm btn-outline-info btn-pill">DETAIL ...</a>';
}
},
{
targets: -1,
render: function (data, type, row) {
if (row['status'] == '1') {
return 'Active'
}
return 'Inactive'
}
},
{
"targets": 'no-sort',
"orderable": false,
},
{
targets: 'hidden',
visible: false,
}
],
"initComplete": function (settings, json) {
$('.no-sort').removeClass('sorting_asc');
table.columns.adjust().draw();
$('.no-sort').removeClass('sorting_asc')
}
});
});
</script>
这里是我的存储库:
public function list($limit = 10, $page = 1, $sort = "", $q = "", $params_extra = []) {
$query = ProductBundle::select();
$query->with('product_bundle_detail.product');
$query->with('hub');
if (!empty($params_extra)) {
$query->where($params_extra);
}
$total = $query->count();
if ($q != "") {
$query->where(function ($query) use ($q) {
return $query->where('name', 'like', "%$q%");
});
}
if ($sort != "") {
$sort = explode(':', $sort);
$query->orderBy($sort[0],@$sort[1]);
}
$total_filtered = $query->count();
$limit = ($limit == -1) ? 10 : $limit;
if (!is_null($limit)) {
$offset = ($page - 1) * $limit;
$query->offset($offset)->limit($limit);
}
$data = $query->get()->map(function($value){
$value = General::encode_ids($value->toArray());
$value['images'] = json_decode($value['images']);
$value['images_thumb'] = json_decode($value['images_thumb']);
if (@$value['product_bundle_detail']) {
foreach ($value['product_bundle_detail'] as $key1 => $product_bundle_detail) {
if (@$product_bundle_detail['product']['images']) {
$value['product_bundle_detail'][$key1]['product']['images'] = json_decode($value['product_bundle_detail'][$key1]['product']['images']);
}
}
}
return $value;
});
return [
'data' => $data,
'total_rows' => $total,
'total_filtered' => $total_filtered,
];
}
我试图搜索代码错误的位置,但看不到问题在哪里。有人能帮我解决这个问题吗?在jQuery/javaScript中使用php变量时,必须像这样将它们括在双大括号中
{{hub->name}}
欢迎来到SO。你试过什么调试?您的关系是否真的适用于像这样包含的
select()
?$total
>0?为什么要抑制错误消息,如@$value
,@$product\u bundle\u detail
?您是否尝试过删除map()
并查看查询结果以检查它是否正常工作?将问题分解为几个小步骤,并测试每个步骤。1) 这种关系。
{{hub->name}}