Я пытаюсь реализовать фильтры для таблицы в моем приложении Laravel.Например, у меня есть таблица «Заказы» со столбцом «Статус заказа».Мне нужна кнопка, которую я могу нажать, чтобы видеть все доставленные заказы (кроме отложенных).Я попробовал дать мне пустую страницу и измененный URL.Это то, что я пытаюсь сделать.
Вид
<form action="{{ action('FilterController@status_filter') }}" method="POST">
@csrf
<button type="submit" name="pending" value="1">Pending</button>
<button type="submit" name="delivered" value="2">Delivered</button>
</form>
@if(count ($info) > 0)
<table>
<thead>
<th>orderNumber</th>
<th>orderDate</th>
<th>shippedDate</th>
<th>status</th>
<th>comments</th>
<th>customerNumber</th>
</thead>
@foreach ($info as $info)
<tr>
<td>{{ $info->orderNumber }}</td>
<td>{{ $info->orderDate }}</td>
<td>{{ $info->shippedDate }}</td>
<td>{{ $info->status }}</td>
<td>{{ $info->comments }}</td>
<td>{{ $info->customerNumber }}</td>
</tr>
@endforeach
@endif
Контроллер
<?php
public function status_filter(Request $request)
{
$filter = FilterModel::where('status', true);
if ($request->has('1')) {
$filter->where('status', $pending);
}
if ($request->has('2')) {
$filter->where('status', $delivered);
}
return $filter->get();
return redirect('filter', ['filter' => $filter]);
}
Маршрут
Route::post('status_filter', 'FilterController@status_filter');
Модель:
class FilterModel extends Model
{
protected $table = 'orders';
protected $primarykey = 'orderNumber';
}
Схема таблицы: