How to get selected values from db in select list Here's my artist-edit.blade.php:
<select name="id_section[]" multiple>
@foreach ($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->title }}</option>
@endforeach
</select>
ArtistController:
public function edit($id)
{
$artist = Artist::find($id);
$tags = Tag::all();
return View::make('admin.artists.edit')->with('artist', $artist)->with('tags', $tags);
}
Artists and Tags has many to many relationship.
How can i get only selected tags from database? E.g. i can get Tag title in edit form this way:
{{ $tag->title }}
The classic way :
public function edit($id)
{
$artist = Artist::with('tags')->find($id);
return View::make('admin.artists.edit', compact('artist'));
}
And you get tags with :
@foreach ($artist->tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->title }}</option>
@endforeach
If you want to use the FormBuilder you can also use list method.
@heihachi88
// prepare
$artist = Artist::find($id);
$tags = Tag::lists('title', 'id');
$selected = $artist->tags->lists('id');
// view, using FormBuilder
{!! Form::select('tags[]', $tags, $selected, [ 'multiple' => 'multiple' ]) !!}
// without FormBuilder
<select name="tags[]" multiple>
@foreach ($tags as $tag)
<option value="{{$tag->id}}" {{ (in_array($tag->id, $selected)) ? ' selected="selected"' : '' }}>{{$tag->title}}</option>
@endforeach
</select>
@JarekTkaczyk thanks again, bro! Seems like with FormBuilder it's much easier. What do you prefer yourself - use formbuilder or not?
@heihachi88 I use it currently. Anyway it's much easier to use some kind of tool for such a repetitive tasks, rather than writing your whole HTML manually.
@foreach ($data['accounttype'] as $key=>$value)
<option value="{{$value->accounttypeid}}"{{$value->accounttypeid == ($accountdetails->accounttypeid)?'selected':''}}>{{$value->accounttypename}}
@endforeach
Select Balance Type
<option value="DR"{{!empty($accountdetails->oppeingbalancetype=='DR')?'selected':''}}>DR
<option value="CR"{{!empty($accountdetails->oppeingbalancetype=='CR')?'selected':''}}>CR
Please sign in or create an account to participate in this conversation.