我试图生成一个简单的下拉列表(Laravel 5),其中每个选项的值是我的businesslocations表行的ID,显示的文本由多列数据组成(address_1,address_2) ,城市)从每一行开始。
在我的控制器中
public function create()
{
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)->get();
return view('client.create')->with(['businesslocations' => $businesslocations]);
}
这是我在create.blade.php
中的位置{!! Form::label('businesslocation_id', 'Business location:') !!}
@foreach($businesslocations as $key => $businesslocation)
{!! Form::select('businesslocation_id', $businesslocations, null, ['class' => 'form-control']) !!}
@endforeach
显示值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>
我正在寻找像这样的值=&#34; id&#34;并显示text =&#34; address_1 address_2 city&#34;对于每个选项。不确定到达那里的语法。
<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>
答案 0 :(得分:0)
使用常规HTML生成选项,效果很好。
@Override
public void onCreate(Bundle savedInstanceState) {
// Request email address
GraphRequest.newMeRequest(
loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject me, GraphResponse response) {
Log.d("TEST", response.toString());
if (response.getError() != null) {
// handle error
} else {
Log.d("TEST", me.toString());
String email = me.optString("email");
String id = me.optString("id");
Log.d("TEST", "Email:" + email);
}
}
}).executeAsync();
LoginButton authButton =(LoginButton) findViewById(R.id.login_button);
List<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
authButton.setReadPermissions(permissions);
}
答案 1 :(得分:0)
查询构建器可以使用&#34;列表&#34;方法 - 所以如果你使用返回business_id的sql作为&#34; id&#34;和地址1 +地址2 +城市为&#34;名称&#34;
SELECT `businesslocation_id` as `id`,
CONCAT_WS(" ", `address1`, `address2`) AS `name` FROM `businesslocation`
然后返回一个列表 - 类似于:
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)
->get()
->lists('name','id');
(This is untested)
然后,Eloquent将知道如何处理该名称,如果您坚持下去,则相应地显示它:
{!! Form::select('businesslocation', $businesslocations, null, ['id'=> 'businesslocation','class' => 'form-control']) !!}
所以基本上如果你想用Laravel方式做,你需要将name和id传递给select下拉列表。
也就是说,您还可以为您的商家位置模型添加一个功能:
public function getFullAddressAttribute()
{
return $this->business->address1 . ' ' . $this->business->address2 . ' ' . $this->business->city;
}
然后
$businesslocations = Businesslocation::where('businesslocations.business_id', '=', \Auth::user()->business_id)
->get()
->lists('fullAddress','id');
希望能引导你朝着正确的方向前进!请注意,我还没有测试过上面的代码。