dongzan7016 2015-07-15 12:50
浏览 46
已采纳

表单选择选项,无法获取值并显示正确的文本

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">{&quot;id&quot;:&quot;3&quot;,&quot;business_id&quot;:&quot;7&quot;,&quot;address_1&quot;:&quot; &quot;,&quot;address_2&quot;:&quot; &quot;,&quot;city&quot;:&quot; &quot;,&quot;created_at&quot;:&quot;2015-07-13 15:59:19&quot;,&quot;updated_at&quot;:&quot;2015-07-13 15:59:19&quot;}</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>
  • 写回答

2条回答 默认 最新

  • dongruolin5324 2015-07-17 05:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类