Reputation: 189
I've created a crud form that has a dropdown box in laravel 5.4. The dropdown box links to the menu, it also has a Please select option which has a value of 0 incase the user doesn't want to link a menu. The problem I'm having is that I can't seem to get the dropdown box to display 'Please Select' when in the edit form.
In my edit form it will display the menu that has been selected else it is supposed to say "Please Select"
So what is supposed to happen is if the user selected a menu item when they created the contact the first time then in the edit form the menu item is supposed to show. Else if the user selected "Please Select" then in the edit form the "Please Select" is supposed to be show.
I hope I made sense.
My 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');
}
}
My 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
Upvotes: 1
Views: 1450
Reputation: 860
Change the below line
{!! Form::select('menu_id', $menu_options->prepend('Please Select', '0'), $selected_options, array("class" => "form-control")) !!}
with
{!! Form::select('menu_id', $menu_options->prepend('Please Select', '0'), (count($selected_options) > 0) ? $selected_options : '0', array("class" => "form-control")) !!}
Upvotes: 1