2016-07-15 176 views
0

我有一個關聯數組$ $集合和一個索引數組$ gnd,我已經從Controller傳遞到Laravel 5.2中的view.blade.php。我想打印這兩個數組的值在一個表中。這是我的代碼,在laravel5.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的新人。請幫我...

功能,從控制器通過兩個數組:

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')); 
} 

回答

0

你正在因爲這兩個數組的大小不相等的錯誤!

你爲什麼開始$j = 1?不應該$j = 0,如果雙方你數組大小相同$j = 0將解決您的問題,改變<td>{{ $j }}</td><td>{{ $j+1 }}</td>

此外,您還可以告訴我們,在發送這個兩個陣列控制器的功能?

+1

感謝theMohammadA。有效。我顯示了我的控制器通過了兩個陣列。再次感謝... –

+0

不客氣:) – theMohammedA