Я пытался исправить это весь день, но я не смог это исправить. Не уверен, что мне не хватает. Любая помощь будет оценена.
RefillRequestController.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\RefillRequest;
use App\Order;
use App\API;
use App\Package;
class RefillRequestController extends Controller
{
public function index()
{
return view('admin.refills.index');
}
public function indexData()
{
$refills = RefillRequest::join('orders', 'refill_requests.order_id', '=', 'orders.id')
->join('apis', 'orders.api_id', '=', 'apis.id')
->select('refill_requests.id as rid', 'refill_requests.status', 'apis.name', 'orders.api_order_id', 'orders.id', 'refill_requests.created_at')
->getQuery()->get();
return datatables()
->of($refills)
->addColumn('details_url', function($refill) {
return url('admin/refills/' . $refill->id . '/details');
})
->addColumn('action', function($refill) {
return view('admin.refills.index-buttons', compact('refill'));
})
->editColumn('status', function($refill) {
return '<span class=\'status-' . str_replace(' ', '', strtolower($refill->status)) . '\'>' . $refill->status . '</span>';
})
->addColumn('api', function($refill) {
return $refill->name;
})
->addColumn('api_order_id', function($refill) {
return $refill->api_order_id;
})
->rawColumns(['action', 'status'])->toJson();
}
public function indexFilter($status)
{
return view('admin.refills.index', compact('status'));
}
public function indexFilterData($status)
{
$status = ($status == 'inprogress' ? 'in progress' : $status);
$refills = RefillRequest::join('orders', 'refill_requests.order_id', '=', 'orders.id')
->join('apis', 'orders.api_id', '=', 'apis.id')
->select('refill_requests.id as rid', 'refill_requests.status', 'apis.name', 'orders.api_order_id', 'orders.id', 'refill_requests.created_at')
->where(['refill_requests.status' => strtoupper($status)])
->getQuery()->get();
return datatables()->of($refills)
->addColumn('details_url', function($refill) {
return url('admin/refills/' . $refill->id . '/details');
})
->addColumn('action', function($refill) {
return view('admin.refills.index-buttons', compact('refill'));
})
->editColumn('status', function($refill) {
return '<span class=\'status-' . str_replace(' ', '', strtolower($refill->status)) . '\'>' . $refill->status . '</span>';
})
->addColumn('api', function($refill) {
return $refill->name;
})
->addColumn('api_order_id', function($refill) {
return $refill->api_order_id;
})
->rawColumns(['action', 'status'])->toJson();
}
public function details(Order $order)
{
$data[] = [
'name' => 'User',
'desc' => '<a href="/admin/users/' . $order->user->id . '/edit">' . $order->user->name . '</a>'
];
$data[] = [
'name' => 'Service',
'desc' => '<a href="/admin/services/' . $order->package->service->id . '/edit">' . $order->package->service->name . '</a>'
];
$data[] = [
'name' => 'Package',
'desc' => '<a href="/admin/packages/' . $order->package->id . '/edit">' . $order->package->name . '</a>'
];
$data[] = [
'name' => 'Link',
'desc' => '<a rel="noopener noreferrer" href="' . getOption('anonymizer') . $order->link . '" target="_blank">' . $order->link . '</a>'
];
$data[] = [
'name' => 'Amount',
'desc' => $order->price
];
$data[] = [
'name' => 'Quantity',
'desc' => $order->quantity
];
$data[] = [
'name' => 'Start Counter',
'desc' => $order->start_counter
];
$data[] = [
'name' => 'Order Date', 'desc' => $order->created_at
];
return datatables()->of($data)->rawColumns(['desc'])->toJson();
}
public function changeStatus(RefillRequest $refill, $status)
{
$str = strtolower($refill->status);
switch ($str) {
case 'pending':
switch ($status) {
case 'start':
$refill->status = 'IN PROGRESS';
break;
case 'cancel':
$refill->status = 'CANCELLED';
$refill->order->status = 'COMPLETED';
break;
case 'complete':
$refill->status = 'COMPLETED';
$refill->order->status = 'COMPLETED';
break;
}
break;
case 'in progress':
switch ($status) {
case 'cancel':
$refill->status = 'CANCELLED';
$refill->order->status = 'COMPLETED';
break;
case 'complete':
$refill->status = 'COMPLETED';
$refill->order->status = 'COMPLETED';
break;
}
break;
}
$refill->order->save();
$refill->save();
return back();
}
}
?>
index.blade.php
<script id="details-template" type="text/x-handlebars-template">
<table class="table details-table mydatatable_details" id="refill-@{{id}}">
</table>
</script>
<script>
var template = Handlebars.compile($("#details-template").html());
var table = $('#mydatatable').DataTable({
processing: true,
serverSide: true,
pageLength: 25,
order: [[1, 'asc']],
ajax: '{!! url($dataURL) !!}',
columnDefs: [
{ targets: [6], className: "dt-right"},
],
columns: [
{
"className": 'details-control',
"orderable": false,
"searchable": false,
"data": null,
"defaultContent": ''
},
{data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'api_order_id', name: 'api_order_id'},
{data: 'status', name: 'status'},
{data: 'created_at', name: 'created_at'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
// Add event listener for opening and closing details
$('#mydatatable').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
var tableId = 'refill-' + row.data().id;
console.log(tableId);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(template(row.data())).show();
initTable(tableId, row.data());
tr.addClass('shown');
tr.next().find('td').addClass('no-padding bg-gray');
}
});
function initTable(tableId, data) {
console.log(data.details_url);
$('#' + tableId).DataTable({
// processing: true,
serverSide: true,
paging: false,
searching: false,
ordering: false,
info: false,
ajax: data.details_url,
columns: [
{ data: 'name', name: 'id' },
{ data: 'desc', name: 'unit' }
]
})
}
</script>
Маршрут
Route::get('/refills/list', 'RefillRequestController@index');
Route::get('/refills-ajax/data', 'RefillRequestController@indexData');
Route::get('/refills-filter/{status}', 'RefillRequestController@indexFilter');
Route::get('/refills-filter-ajax/{status}/data', 'RefillRequestController@indexFilterData');
Route::get('/refills/{order}/details', 'RefillRequestController@details');
Route::get('/refill/{refill}/{status}', 'RefillRequestController@changeStatus');
APP / RefillRequest.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class RefillRequest extends Model
{
protected $guarded = [];
public function order()
{
return $this->belongsTo(Order::class);
}
public function getStatusAttribute($status)
{
return title_case($status);
}
}
?>
Теперь я попытался исправить код, но, к сожалению, в таблице данных отсутствуют данные, доступные в таблице. MYSQL содержит данные в таблице с именем refill_requests и имеет структуру http://prntscr.com/krxj1t