Laravel如何在更新特定资源时使用刀片在下拉列表中选择一个值

时间:2017-10-06 01:57:07

标签: php laravel laravel-5.4 blade laravel-blade

我想在编辑表格中的特定资源时回显所选值。当我编辑特定资源时,它应该在我的下拉列表中显示当前数据,但在真实场景中它会显示列表中的第一个错误。那么如何使用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语句?但是如何在我的选择表单中使用刀片模板来完成它。有人可以帮帮我吗?

感谢有人可以提供帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

以下是一个例子:

打开表格:

{{ 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');

(将数据传递到您的视图时包含此内容)

相关问题