I'm trying to generate a simple drop-down list (Laravel 5) where the value of each option is the ID of my businesslocations table rows, and the displayed text is composed of multiple columns data (address_1, address_2, city) taken from each row.
In my controller
public function create()
{
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)->get();
return view('client.create')->with(['businesslocations' => $businesslocations]);
}
Here's where i'm at in create.blade.php
{!! Form::label('businesslocation_id', 'Business location:') !!}
@foreach($businesslocations as $key => $businesslocation)
{!! Form::select('businesslocation_id', $businesslocations, null, ['class' => 'form-control']) !!}
@endforeach
It displays with value 0 :
<label for="businesslocation_id">Business location:</label>
<select class="form-control" id="businesslocation_id" name="businesslocation_id"><option value="0">{"id":"3","business_id":"7","address_1":" ","address_2":" ","city":" ","created_at":"2015-07-13 15:59:19","updated_at":"2015-07-13 15:59:19"}</option></select>
I'm looking for something like this value="id" and display text="address_1 address_2 city" for each option. Not sure about the syntax to get there.
<label for="businesslocation_id">Business location:</label>
<select class="form-control" id="businesslocation_id" name="businesslocation_id"><option value="1">Business location 1 - address_1 address_2 city</option><option value="2" selected="selected">Business location 2 - address_1 address_2 city</option></select>
The query builder can use a "lists" method - so if you used sql that returned business_id as "id" and address1 + address2 + city as "name"
SELECT `businesslocation_id` as `id`,
CONCAT_WS(" ", `address1`, `address2`) AS `name` FROM `businesslocation`
And then returned a list - similar to:
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)
->get()
->lists('name','id');
(This is untested)
Then Eloquent will know what to do with that name , id and display it accordingly if you stick to :
{!! Form::select('businesslocation', $businesslocations, null, ['id'=> 'businesslocation','class' => 'form-control']) !!}
So basically if you want to do it the Laravel way you need to pass name and id to the select dropdown.
That said, you could also add a function to your businesslocation model:
public function getFullAddressAttribute()
{
return $this->business->address1 . ' ' . $this->business->address2 . ' ' . $this->business->city;
}
and then
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)
->get()
->lists('fullAddress','id');
Hope that steers you in the right direction! Just a note that I haven't tested the code above.
Used regular HTML to generate the options, works great.
{!! Form::label('businesslocation_id', 'Business location:') !!}
<select class="form-control" id="businesslocation_id" name="businesslocation_id">
@foreach($businesslocations as $key => $businesslocation)
<option value="{{$businesslocation->id}}">{{$businesslocation->address_1}} {{$businesslocation->address_2}} {{$businesslocation->city}}</option>
@endforeach
</select>