[英]Form select option, can't get the value and display text right
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. 我正在尝试生成一个简单的下拉列表(Laravel 5),其中每个选项的值是我的businesslocations表行的ID,并且显示的文本由从中获取的多列数据(地址_1,地址_2,城市)组成每一行。
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 这是我在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 : 它显示为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. 我正在寻找类似此值=“ id”的内容,并为每个选项显示text =“ address_1 address_2 city”。 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>
Used regular HTML to generate the options, works great. 使用常规HTML生成选项,效果很好。
{!! 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>
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" 查询构建器可以使用“列表”方法-因此,如果您使用返回business_id作为“ id”和返回address1 + address2 + city作为“ name”的sql
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 : 然后,Eloquent将知道该名称和id的处理方式,如果您坚持使用,则会相应地显示它:
{!! 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. 因此,基本上,如果您想使用Laravel方式,则需要将名称和ID传递给选择下拉列表。
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.
请注意,我尚未测试上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.