【问题标题】:I want to display the entire list of countries in the Select Options menu in laravel我想在 laravel 的“选择选项”菜单中显示整个国家列表
【发布时间】:2018-09-06 03:24:03
【问题描述】:

我想在 laravel 的“选择选项”菜单中显示整个国家/地区列表。

这是我的数据: this my data

这个控制器

公共函数 getCart()

        {
            $country = Roketin::country()->list()->get();
            // dd($country);
            $province = Roketin::province()->list('country_code')->get();
            // dd($province);
            $carts = Cart::getContent();
            return view('pay-viewcart', compact('carts', 'country', 'province'));
        }      

这是我的blade.php

*国家

    <select class="selectpicker" id="country" name="country">
          <option> -- Select One -- </option>
                  @foreach($country->data[0]->name as $countries)
                          <option> {{$countries}} </option>   
                  @endforeach                                            
     </select>

和错误: 为 foreach() 提供的参数无效(查看:D:\PPI\projek-1\savanacamp\resources\views\pay-viewcart.blade.php)"

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:
    <select class="selectpicker" id="country" name="country">
        <option> -- Select One -- </option>
        @foreach($country->data as $item)
            <option> {{$item->name}} </option>
        @endforeach
    </select>
    

    <select class="selectpicker" id="country" name="country">
        <option> -- Select One -- </option>
        @foreach($country->data as $item)
            <option> {{$item['name']}} </option>
        @endforeach
    </select>
    

    【讨论】:

    • 我更改了这个,并且出现错误“尝试获取非对象的属性(查看:D:\PPI\projek-1\savanacamp\resources\views\pay-viewcart.blade. php)"
    • 将 {{$item->name}} 更改为 {{$item['name']}}
    • 你可能需要使用:@foreach($country->data as $item) 或 @foreach($country['data'] as $item)
    • 如果我试试这个 @foreach($country['data'] as $item) 。错误:“不能使用 stdClass 类型的对象作为数组”
    【解决方案2】:

    您确实在foreach 中提供了错误的值

    如下更新你的 foreach。

    <select class="selectpicker" id="country" name="country">
        <option> -- Select One -- </option>
        @foreach($country as $c)
        <!--        ^--- supply the list here.  -->
            <option> {{ $c->data[0]->name }} </option>   
        <!--                   ^--- access the name here -->
        @endforeach                                               
    </select>
    

    【讨论】:

    • 我改变了这个,并且出现错误“试图获取非对象的属性(查看:D:\PPI\projek-1\savanacamp\resources\views\pay-viewcart.blade. php)"
    • 更新名称的访问,因为我不知道你的模型是什么样子的。或 $country 数组/列表的样子。请dd它自己看看。我们在这里要回答的主要问题是您对 foreach 的错误构造。要么更新您的问题,让我们获得有关您问题的更新信息,要么查看这些错误并尝试找出您出错的地方。 :)
    • 我的问题中上面列出的数据数组。格式是图片
    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多