简体   繁体   中英

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?

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? 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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