Laravel 5 Eloquent - 使用列名列表从db动态检索数据

时间:2015-09-21 14:06:51

标签: mongodb laravel-5 eloquent blade

我有这个函数从MongoDB文档中检索字段名称。

// Returns an array of field names from a collection of Plan models.
        $keys = DB::collection('plans')->raw(function($collection)
        {
            $cursor = $collection->find();
            $array = iterator_to_array($cursor);
            $fields = array();
            foreach ($array as $k=>$v) {
                foreach ($v as $a=>$b) {
                    $fields[] = $a;
                }
            }
            // print_r($fields);
            // print_r(array_values(array_unique($fields)));    
            return array_values(array_unique($fields));    
        });

在我看来,我试图为每个订阅计划打印所有值。我做了一个循环来遍历$plans并在其中我再做一个循环遍历$keys变量:

{{ $plan->$key }}

我收到此错误:

ErrorException in helpers.php line 454: htmlentities() expects parameter 1 to be string, array given (View: /var/www/myproject/resources/views/acquisti/acquisti.blade.php)

我用PHP的gettype检查过,数组中的元素是字符串,那么问题出在哪里呢?

谢谢!

编辑:acquisti.blade.php视图的代码:

<ul class="pricing_table">
<li class="price_block legenda">
    <h3 class="padBot">Dettagli</h3>
    <ul>
        <li>{{ trans('Duration') }}</li>
        <li>{{ trans('Included quotes') }}</li>
        <li>{{ trans('Additional subcategories') }}</li>
        <li>{{ trans('Multicategory') }}</li>
        <li>{{ trans('Number of regions') }}</li>
        <li>{{ trans('Multioffice') }}</li>
        <li>{{ trans('Banner') }}</li>
        <li>{{ trans('Quotes per month') }}</li>
        <li>{{ trans('Dedicated web page') }}</li>
        <li>{{ trans('Bonus') }}</li>
        <li>{{ trans('Included tag') }}</li>
        <li>{{ trans('Multisubcategory ') }}</li>
        <li>{{ trans('Number of selections') }}</li>
        <li>{{ trans('Sponsored Ad') }}</li>
        <li>{{ trans('Price of second multicategory') }}</li>
        <li>{{ trans('Price of third multicategory') }}</li>
        <li>{{ trans('Request of additional information') }}</li>
        <li>{{ trans('Quotes Fido') }}</li>
        <li>{{ trans('Price for additional regions') }}</li>
        <li>{{ trans('Buy the entire region') }}</li>
        <li>{{ trans('Number of provinces') }}</li>
        <li>{{ trans('Price for additional provinces') }}</li>
        <li>{{ trans('Buy the entire province') }}</li>
        <li>{{ trans('Number of postal codes') }}</li>
        <li>{{ trans('Price for additional postal codes') }}</li>
        <li>{{ trans('Number of municipalities') }}</li>
        <li>{{ trans('Price for additional municipalities') }}</li>
        <li>{{ trans('Sponsored ad L1') }}</li>
        <li>{{ trans('Sponsored ad L2') }}</li>
        <li>{{ trans('Sponsored ad L3') }}</li>
        <li>{{ trans('Standard support') }}</li>
        <li>{{ trans('Dedicated support') }}</li>
        <li>{{ trans('Paid support') }}</li>
        <li>{{ trans('Price for 1 credit') }}</li>
        <li>{{ trans('Max bulk purchase of credits') }}</li>
        <li>{{ trans('Payments') }}</li>
    </ul>
</li>

@foreach($plans as $plan)
    <li class="price_block">
        @if($plan->name == "Business")
            <h3>{{ $plan->name }} <small>{{ $plan->type }}</small></h3>
        @else
            <h3 class="padBot">{{ $plan->name }}</h3>
        @endif
        <div class="price">
            <div class="price_figure">
                <span class="price_number"></span>
                <span class="price_tenure"></span>
            </div>
        </div>
        <ul class="features">
        @foreach($keys as $key)
            @if($key != "_id" && $key != "name")
                <!-- <?php print gettype($key); ?> -->
                <li id="{{ $key }}">{{ $key }}</li>
            @endif
        @endforeach
        </ul>
        <div class="footer">
            <a href=""></a>
        </div>
    </li>
@endforeach

0 个答案:

没有答案