简体   繁体   English

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

[英]Laravel how to echo selected a value in a dropdown using blade when updating a specific resource

I want to echo the selected value when I edit a specific resource in my table.我想在编辑表中的特定资源时回显所选值。 When I edit the specific resource it should display the current data in my dropdown but in the real scenario it displays the first one on the list which is wrong.当我编辑特定资源时,它应该在我的下拉列表中显示当前数据,但在实际情况下,它显示列表中第一个错误的数据。 So how can I echo the selected value in the options of the dropdown using blade in laravel?那么如何在 laravel 中使用刀片在下拉选项中回显选定的值呢?

Here is some sample of my code in the view below这是我在下面视图中的一些代码示例

<!-- 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>

and here is the code in my controller below.这是下面我的控制器中的代码。

 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);
    }

Everything here works fine even if I updated the specific resource.即使我更新了特定资源,这里的一切都正常。 I just want to auto populate or select the current value of the resource that I will update because when I edit the specific resource it shows the first one on the list.我只想自动填充或选择我将更新的资源的当前值,因为当我编辑特定资源时,它会显示列表中的第一个。

Am I going to use an @if statement in blade?我要在刀片中使用 @if 语句吗? But how can I do it using the blade template in my select form.但是我如何在我的选择表单中使用刀片模板来做到这一点。 Can somebody help me?有人可以帮助我吗?

Appreciate if someone can help.感谢有人可以提供帮助。 Thanks in advance.提前致谢。

Here is an example:下面是一个例子:

Open form:打开表格:

{{ Form::model($service, array('route' => array('services.update', $service->id))) }}

Select form field:选择表单域:

<div class="form-group">
    {{ Form::label('Related Agreement') }}
    {{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>

In Controller:在控制器中:

$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');

(Include this when passing data to your view) (在将数据传递到您的视图时包括此内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM