Laravel 如何在更新特定资源时使用 blade 回显在下拉列表中选择的值
Laravel how to echo selected a value in a dropdown using blade when updating a specific resource
我想在 table 中编辑特定资源时回显 selected 值。当我编辑特定资源时,它应该在我的下拉列表中显示当前数据,但在实际情况下,它显示列表中的第一个错误。那么如何在 laravel 中使用 blade 回显下拉选项中的 selected 值?
下面视图中有一些我的代码示例
<!-- 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);
}
即使我更新了特定资源,这里的一切都工作正常。我只想自动填充或 select 我将更新的资源的当前值,因为当我编辑特定资源时,它会显示列表中的第一个。
我要在 blade 中使用 @if 语句吗?但是我如何在我的 select 表单中使用 blade 模板来做到这一点。有人可以帮助我吗?
如果有人能提供帮助,我们将不胜感激。
提前致谢。
这是一个例子:
打开表格:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select 表单域:
<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');
(将数据传递到您的视图时包括此内容)
我想在 table 中编辑特定资源时回显 selected 值。当我编辑特定资源时,它应该在我的下拉列表中显示当前数据,但在实际情况下,它显示列表中的第一个错误。那么如何在 laravel 中使用 blade 回显下拉选项中的 selected 值?
下面视图中有一些我的代码示例
<!-- 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);
}
即使我更新了特定资源,这里的一切都工作正常。我只想自动填充或 select 我将更新的资源的当前值,因为当我编辑特定资源时,它会显示列表中的第一个。
我要在 blade 中使用 @if 语句吗?但是我如何在我的 select 表单中使用 blade 模板来做到这一点。有人可以帮助我吗?
如果有人能提供帮助,我们将不胜感激。 提前致谢。
这是一个例子:
打开表格:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select 表单域:
<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');
(将数据传递到您的视图时包括此内容)