[英]Laravel how to echo selected a value in a dropdown using blade when updating a specific resource
我想在編輯表中的特定資源時回顯所選值。 當我編輯特定資源時,它應該在我的下拉列表中顯示當前數據,但在實際情況下,它顯示列表中第一個錯誤的數據。 那么如何在 laravel 中使用刀片在下拉選項中回顯選定的值呢?
這是我在下面視圖中的一些代碼示例
<!-- Shows Field -->
<div class="form-group col-sm-6">
{!! Form::label('show_id', 'Shows:') !!}
{!! Form::select('show_id', $shows, $shows, ['class' => 'form-control input-md','required'])!!}
</div>
{{-- {{ $channelshows->channel->name }} --}}
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('channel_id', 'Channels:') !!}
{!! Form::select('channel_id', $channel, $channel, ['class' => 'form-control input-md','required'])!!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<a href="{!! route('admin.channelshows.index') !!}" class="btn btn-default">Cancel</a>
</div>
這是下面我的控制器中的代碼。
public function edit($id)
{
$channelshows = $this->channelshowsRepository->findWithoutFail($id);
$shows = Show::pluck('name', 'id');
$channel = Channel::pluck('name', 'id');
if (empty($channelshows)) {
Flash::error('Assigned show not found');
return redirect(route('admin.channelshows.index'));
}
return view('admin-dashboard.channelshows.edit', compact('shows', $shows), compact('channel', $channel))->with('channelshows', $channelshows);
}
即使我更新了特定資源,這里的一切都正常。 我只想自動填充或選擇我將更新的資源的當前值,因為當我編輯特定資源時,它會顯示列表中的第一個。
我要在刀片中使用 @if 語句嗎? 但是我如何在我的選擇表單中使用刀片模板來做到這一點。 有人可以幫助我嗎?
感謝有人可以提供幫助。 提前致謝。
下面是一個例子:
打開表格:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
選擇表單域:
<div class="form-group">
{{ Form::label('Related Agreement') }}
{{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>
在控制器中:
$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
(在將數據傳遞到您的視圖時包括此內容)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.