我有一个搜索表,用于搜索负责人,日期和设备名称。当有人搜索带有日期的仓位时,我希望该搜索数据保留在结果中
整个搜索表单
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
@csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
@foreach($users as $user)
<option>{!! $user->name !!}</option>
@endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white"><a href="{{route('reviews.index')}}">Reset</a></button>
{{ Form::close() }}
还有Controller中的索引方法
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
谢谢
答案 0 :(得分:1)
您可以使用old()
函数用上次提交的数据重新填充表单。
请参见https://laravel.com/docs/5.6/requests#old-input
因此,在您的刀片服务器代码中,将是这样的:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
对于select,它是不同的,基本上,您比较循环中的每个呈现的选项,如果是提交的,则将其标记为html。
<select name="responsable">
<option></option>
@foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
@endforeach
</select>
另外,考虑到old()
也接受第二个参数作为默认值,以防您不希望它只是 null 。