Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

toneymutinda's avatar

Retrieving Selected Values From Database Into a Multiselect

Hello, I am working on a reservation system for a hotel using laravel. My models include hotel, room and amenities. A room belongs to a hotel whereas a room can have many amenities. Now, while editing the room model, I have a select box of all the hotels as well as a multiple select of all the amenities. However, i need the hotel and amenities fields to be populated with the relevant data for the hotel while editing. How can i achieve this?

Thank you.

Here's the code

edit.blade.php

@extends('backend/layouts/template')

@section('title')
    Edit Room
@endsection

@push('dashboard-styles')
    <link href="{{ asset('css/plugins/chosen/bootstrap-chosen.css') }}" rel="stylesheet">
    <link href="{{ asset('css/plugins/select2/select2.min.css') }}" rel="stylesheet">
    <link href="{{ asset('css/plugins/jasny/jasny-bootstrap.min.css') }}" rel="stylesheet">
@endpush

@section('page-header')
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>Edit Room</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="">Home</a>
                </li>
                <li>
                    <a>Hotel Management</a>
                </li>
                <li>
                    <a>Rooms</a>
                </li>
                <li class="active">
                    <strong>Edit {{ucfirst($room->name) }} Room</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>
@endsection

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox-float-e-margins">
                <div class="ibox-title">
                     <h5>{{ $room->name }}'s details</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="#">Config option 1</a>
                                </li>
                                <li><a href="#">Config option 2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-12 b-r">
                            <form action="{{ route('rooms.update', $room->id) }}" method="POST" enctype="multipart/form-data" >

                                {{ csrf_field() }}
                                {{ method_field('PATCH') }}

                                <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                                   <label>Name</label> 
                                    <input type="text" value="{{ $room->name }}" name="name" class="form-control" required>
                                    <!-- If first name has error -->
                                    @if ($errors->has('name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('name') }}</strong>
                                        </span>
                                    @endif
                               </div>
                               <div class="form-group{{ $errors->has('hotel_id') ? ' has-error' : '' }}">
                                    <label>Hotel</label> 
                                    <select class="chosen-select form-control" data-placeholder="Select Hotel" id="hotel_id" name="hotel_id" tabindex="2" required>
                                        <option></option>
                                        @foreach($hotels as $hotel)
                                            <option value="{{ $hotel->id }}">{{ $hotel->name }}</option>
                                        @endforeach
                                    </select>
                                    <!-- If first name has error -->
                                    @if ($errors->has('hotel_id'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('hotel_id') }}</strong>
                                        </span>
                                    @endif
                               </div>
                               <div class="form-group{{ $errors->has('amenities') ? ' has-error' : '' }}">
                                   <label>Amenities</label> 
                                    <select class="chosen-select form-control" data-placeholder="Select Amenity(ies)" id="amenities" name="amenities[]" tabindex="2" required multiple="multiple">
                                        <option></option>
                                        @foreach($amenities as $amenity)
                                            <option value="{{ $amenity->id }}">{{ $amenity->name }}</option>
                                        @endforeach
                                    </select>
                                    <!-- If first name has error -->
                                    @if ($errors->has('amenities'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('amenities') }}</strong>
                                        </span>
                                    @endif
                               </div>
                               <div class="form-group{{ $errors->has('price') ? ' has-error' : '' }}">
                                   <label>Price</label> 
                                    <input type="text" placeholder="Price per night" name="price" class="form-control" required>
                                    <!-- If first name has error -->
                                    @if ($errors->has('location'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('price') }}</strong>
                                        </span>
                                    @endif
                               </div>
                               <div class="form-group{{ $errors->has('no_of_persons') ? ' has-error' : '' }}">
                                   <label>Number of people room can accomodate</label> 
                                    <input type="number" placeholder="eg 5" name="no_of_persons" class="form-control" required>
                                    <!-- If first name has error -->
                                    @if ($errors->has('no_of_persons'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('no_of_persons') }}</strong>
                                        </span>
                                    @endif
                               </div>
                               <div class="fileinput fileinput-new input-group form-group{{ $errors->has('image') ? ' has-error' : '' }}" data-provides="fileinput">
                                    <div class="form-control" data-trigger="fileinput">
                                        <i class="fa fa-file fileinput-exists"></i>
                                    <span class="fileinput-filename"></span>
                                    </div>
                                    <span class="input-group-addon btn btn-default btn-file">
                                        <span class="fileinput-new">Select file</span>
                                        <span class="fileinput-exists">Change</span>
                                        <input type="file" name="image" required/>
                                    </span>
                                    <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
                                    <!-- If first name has error -->
                                    @if ($errors->has('image'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('image') }}</strong>
                                        </span>
                                    @endif
                                </div> 
                               <div class="form-group{{ $errors->has('room_information') ? ' has-error' : '' }}">
                                    <label>Room information</label> 
                                    <textarea name="room_information" placeholder="Say more about the room" rows="10" class="form-control" required></textarea>
                                    <!-- If first name has error -->
                                    @if ($errors->has('description'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('description') }}</strong>
                                        </span>
                                    @endif
                               </div>
                                <div>
                                    <!-- <button type="submit" class="btn btn-sm btn-block btn-primary pull-right">Update</button> -->
                                    <button type="submit" class="btn btn-primary">Update</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </div>
    <br>
@endsection

@push('dashboard-scripts')
    <!-- Chosen -->
    <script src="{{ asset('js/plugins/chosen/chosen.jquery.js') }}"></script>
    <!-- Select2 -->
    <script src="{{ asset('js/plugins/select2/select2.full.min.js') }}"></script>
    <!-- Jasny -->
    <script src="{{ asset('js/plugins/jasny/jasny-bootstrap.min.js') }}"></script>

    <script>
        $(document).ready(function(){

           $('.chosen-select').chosen({width: "100%"});

        });
    </script>
@endpush

RoomController.php

 public function edit($id)
    {
        $room = Room::find($id);
        $amenities = Amenity::all();
        $hotels = Hotel::all();
        return view('backend/rooms/edit', compact('room', 'amenities', 'hotels'));
    }
0 likes
10 replies
Snapey's avatar

You would make your life a lot easier by having the user select the hotel first then click a button to add a room to it.

You can then load the amenities applicable to that hotel and send them to the view

RamjithAp's avatar

Use this {!! Form::select( 'amenities[]', $amenities, $room->amenities, ['multiple' => true] ) !!}

Instead of this < select class="chosen-select form-control" data-placeholder="Select Amenity(ies)" id="amenities" name="amenities[]" tabindex="2" required multiple="multiple" > < option >< /option > @foreach($amenities as $amenity) < option value="{{ $amenity->id }}">{{ $amenity->name }}</ option > @endforeach

Snapey's avatar

@RamjithAp why would he suddenly switch to Laravel Collective forms? Besides, that does not address the issue

RamjithAp's avatar

Okay then try using this solution

@foreach($amenities as $amenity)
<option <?php if (in_array($amenity->id, $room->amenities)) { echo "selected"; } ?> value="{{ $amenity->id }}">{{ $amenity->name }}</option> 
@endforeach
Snapey's avatar

i'm sorry, i could have sworn you said

However, i need the hotel and amenities fields to be populated with the relevant data for the hotel while editing.

but in the controller you grab ALL amenities not just those for this hotel. How does the person adding the room know what is applicable for that hotel.

From a ux point of view, i think users struggle with the concept of multi-select and you would be better with a grid of checkboxes

toneymutinda's avatar

@Snapey A hotelhas no rlnship with amenities. Meaning that an amenity, such as wifi or sauna can only be applicable to a room.

When editing i think i'll have to go for the checkboxes for the amenities. That way a room can have more than one amenity

Snapey's avatar

so you want All amenities listed. If only one hotel has something obscure like a microwave in each room, you then need to add microwave to your form so that it is selectable for ANY room in any hotel for ever more. Gradually the list of amenities will get longer and longer with obscure choices

toneymutinda's avatar

@Snapey Yeah all amenities will be listed but the ones belonging to that particular room will be checked automatically

Hotel.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Hotel extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'location', 'image', 'description',
    ];

    /**
     * One to Many relationship with room
     *
     */
    public function rooms()
    {
        return $this->hasMany('App\Room');
    }
}

Room.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Room extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'image', 'room_information', 'price', 'no_of_persons', 'reserved', 'hotel_id',
    ];

    /**
     * One to Many relationship with room
     *
     */
    public function hotel()
    {
        return $this->belongsTo('App\Hotel');
    }

    /**
     * Many to Many relationship with amenity
     * Room can have many amenities
     *
     */
    public function amenities()
    {
        return $this->belongsToMany('App\Amenity');
    }
}

Amenity.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Amenity extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
    ];

    /**
     * Many to Many relationship with room
     * Amenity can belong to many rooms
     *
     */
    public function rooms()
    {
        return $this->belongsToMany('App\Room');
    }
}

Please or to participate in this conversation.