在laravel5.2中的view.blade.php中打印索引数组值

时间:2016-07-15 12:26:51

标签: laravel-5.2

我有一个关联数组$ collection和一个索引数组$ gnd,我已经从Controller传递给了Laravel 5.2中的view.blade.php。我想在一个表中打印两个数组的值。这是我的代码,

<table class="responsive-table highlight centered">
    <thead>
        <tr>
            <th data-field="id">Sl.no</th>
            <th data-field="name">Unique </th>
            <th>Name</th>
            <th data-field="price">Description</th>
            <th>Gender</th>
        </tr>
    </thead>

   <tbody>
       {{-- */$j = 1;/* --}}
        @foreach($collection as $key=>$value)
            <tr>
                <td>{{ $j }}</td>
                <td>{{ $value->uid }}</td>
                <td>{{ $value->name }}</td>
                <td>{{ $value->desc }}</td>
                <td>{{ $gnd[$j] }}</td>
                {{-- */$j++;/* --}} 
                @endforeach 
            </tr>
    </tbody>
</table>

对于{{$ gnd [$ j]}}我收到以下错误。

ErrorException in b7c1ab515a44988c31e1982a3ce014434e97ef2c.php line 30:
Undefined offset: 22 (View: /var/www/html/anudip/resources/views/plugins/entries/view.blade.php)

我是laravel的新人。请帮帮我......

从Controller传递两个数组的函数:

public function getDetails(){
    $collection = DB::table('entry_transactions')
                  ->leftJoin('entry_masters', 'entry_transactions.entry_id', '=', 'entry_masters.id')
                  ->get(['entry_masters.id as uid','entry_masters.name as name','entry_masters.gender as gender','entry_transactions.entry_desc as desc']);

$gnd = array(); 
$len = sizeof($gnd);
$i = 0;

foreach ($collection as $key) {

    if($key->gender == 0){
        $gnd[$i] = "Male";
    }   
    else {
        $gnd[$i] = "Female";
    }
    $i++;
}

    return view($this->url.'entries.view', compact('collection','gnd'));
}

1 个答案:

答案 0 :(得分:0)

你得到的错误是因为数组的大小都不相等!

你为什么要开始$j = 1?不应该是$j = 0,如果两个数组的大小相同$j = 0将解决您的问题并将<td>{{ $j }}</td>更改为<td>{{ $j+1 }}</td>

另外,你能告诉我们你发送这两个阵列的控制器功能吗?