'Editing drop-down doesn't fetch value
I have created a drop-down for categories and subcategory. It works fine when i submit the form, but when I edit the form, category field does not come with refilled data from the database, category drop-down come like it show in create form.
here is my edit:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
@foreach($s as $k)
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
@endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
// get the event
$event = Event::findOrFail($id);
$s = Category::all()->where('parent_id','=','0');
$r = Event::all();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
// show the edit form and pass the event
return view('event.edit',compact('event','s','r','daysOfWeek'));}
I haven't used relations for the dropdown, I have used jquery and ajax to select subcategory after I select category. What can i do to get the value stored in database when I do edit form?
Solution 1:[1]
I got my answer thanks every one for help!
View:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
@foreach($s as $k)
@if($k['id'] == $m)
<option value="{{ $k['id'] }}" selected="{{$m}}">{{ $k['name'] }}</option>
@else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
@endif
@endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
@foreach($subcat as $k)
@if($k['name'] == $subid)
<option value="{{ $k['id'] }}" selected="{{$subid}}">{{ $k['name'] }}</option>
@else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
@endif
@endforeach
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
$event = Event::findOrFail($id);
$s = Category::where('parent_id','=','0')->get();
$r = Event::all();
$m = Event::find($id)->category;
$subid = Event::find($id)->subcategory;
$subcat = Category::where('parent_id','=',$m)->get();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
return view('event.edit',compact('event','s','subcat','subid','r','daysOfWeek','m'));
}
Solution 2:[2]
There is a mistake in your Controller method edit()
Your code:
$s = Category::all()->where('parent_id','=','0'); // wrong
Isn't that supposed to be:
$s = Category::where('parent_id','=','0')->get(); // correct
You are already fetching the categories with all, and then passing the where condition, hence it is not showing you the results. where condition returns the instance of Illuminate\Database\Eloquent\Builder and not the desired results.
Update the wrong line with the correct one and the result should appear as you want.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shweta |
| Solution 2 | halfer |
