2016-07-15 2 views
0

私は、Laravel 5.2のControllerからview.blade.phpに渡した1つの連想配列$ collectionと1つのインデックス配列$ gndを持っています。私は両方の配列の値を1つのテーブルに出力したい。ここに私のコードは、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をすべきではない、両方のあなたの配列はあなたの問題を解決し、<td>{{ $j }}</td>また

<td>{{ $j+1 }}</td>に、あなたは私たちに、この二つの配列を送信しているあなたのコントローラ機能を表示することができます変更されます同じサイズ$j = 0を持っている場合はどうなりますか?

+1

ありがとうtheMohammadA。出来た。私は2つの配列を渡す私のコントローラを示しています。もう一度ありがとう... –

+0

あなたは大歓迎です:) – theMohammedA

関連する問題