获取请在laravel的下拉框中选择

时间:2017-06-26 07:37:49

标签: laravel laravel-5

我创建了一个crud表单,在laravel 5.4中有一个下拉框。下拉框链接到菜单,它还有一个请选择选项,其值为0,此时用户不想链接菜单。我遇到的问题是,在编辑表单中,我似乎无法让下拉框显示“请选择”。

在我的编辑表单中,它将显示已选择的菜单,否则应该说“请选择”

所以应该发生的是,如果用户在第一次创建联系人时选择了一个菜单项,那么在菜单项应该显示的编辑表单中。否则,如果用户选择“请选择”,则在编辑表格中应显示“请选择”。

我希望我有道理。

我的ContactController

public function edit($id)
    {
        $contact = Contact::find($id);

        $menu_options = Menu::pluck('title', 'id');

        $menu_item_id = Menu::find($id);

        $selected_options = $contact->menu()
                            ->select('menus.id')
                            ->pluck('id')
                            ->toArray();

        if(is_null($contact))
        {
            return redirect()->route('contact.edit');
        }

        return view('contact::admin.edit', compact('contact', 'menu_item_id', 'selected_options', 'menu_options'));
    }

    public function update($id)
    {
        $input = Input::all();

        $validation = Validator::make($input, Contact::$rules);

        if($validation->fails())
        {
            return redirect()->route('contact.edit')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        if($validation->passes())
        {
            $contact = Contact::FindOrFail($id);

            $menuId = (array) array_get($input, 'menu_id');

            $contact->fill($input)->save();

            $contact->menu()->associate($menuId);

            return redirect()->route('contact.index')->with('success', 'Frame has been updated');
        }
    }

我的edit.blade.php

<!-- This gets the admin template from app/modules/templates/views -->
@extends('templates::layouts.admin')
<!-- This inserts the content below into the template -->
@section('content')

    <div class="row">
        <div class="col-lg-12">
            @if($errors->any())
                <ul>
                    {!! implode('', $errors->all('<li class="error">:message</li>')) !!}
                </ul>
            @endif

            {!! Form::model($contact, array('method' => 'PATCH', 'route' => array('contact.update', $contact->id), 'class' => 'add-form')) !!}
                <div class="form">
                    <div class="form_input">
                        <div>
                            {!! Form::label('menu_id', 'Menu') !!}
                        </div>
                        <div>
                            {!! Form::select('menu_id', $menu_options->prepend('Please Select', '0'), $selected_options, array("class" => "form-control")) !!}
                        </div>
                    </div>

                    <div class="form_input">
                        <div>
                            {!! Form::label('title', 'Title') !!}
                        </div>
                        <div>
                            {!! Form::text('title', $contact->title, array('id' => 'title', 'class' => 'form-control')) !!}
                        </div>
                    </div>

                    <div class="form_input">
                        <div>
                            {!! Form::label('content', 'Content') !!}
                        </div>
                        <div>
                            {!! Form::textarea('content', $contact->content, array('id' => 'content', 'class' => 'form-control')) !!}
                        </div>
                    </div>

                    <div class="submit_button">
                        <div>
                            {!! Form::submit('Submit', array('class' => 'btn btn-info submit', 'role' => 'button')) !!}
                        </div>
                    </div>
                </div>
            {!! Form::close() !!}
        </div>
    </div>
@stop   

1 个答案:

答案 0 :(得分:1)

更改以下行

{!! Form::select('menu_id', $menu_options->prepend('Please Select', '0'), $selected_options, array("class" => "form-control")) !!}

{!! Form::select('menu_id', $menu_options->prepend('Please Select', '0'), (count($selected_options) > 0) ? $selected_options : '0', array("class" => "form-control")) !!}