samedi 22 juillet 2017

Laravel Select Field: How to get selected the saved value of a Model

On the Edit view I have a Select field and I want that select field to have the saved value for a given Model to be selected.

mediaController.php

public function edit($id)
{
    //
     $media = Media::find($id);
     $categories = Category::lists('category', 'id');
     return view('medias.edit-media')->with('media', $media)->with('categories', $categories);
}

edit.blade.php

   <div class="form-group">
       {!! Form::select('categories', $categories, $media->category ) !!}
   </div>

On the index view (i.e the first Media as a Category of Video)

enter image description here

On the edit view (the first Media doesn't have the Category 'Video' selected)

enter image description here

even if I change my edit.blade.php to this: ...

<div class="form-group">
    <label>Category of Upload
        <select name="category" id="category" class="form-control input-sm">
            @foreach($categories as $category)
            <option value="" ></option>

            @endforeach
           </select>
    </label>

</div>

I still have the same result (the right Category is not selected)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire