2017-03-02 5 views
0

みなさん、こんにちは、私は、配列througループしたいときに問題を取得してIMとlaravelブレードエンジンとそれを表示する取得配列のキー

私のコントローラのコードは、この

$table = DB::table('tables')->select('id')->where('name', strtolower($name))->first(); 

    $columns = Column::where('table_id', $table->id)->get(); 

    foreach ($columns as $col) { 
     $data[] = $col->col_name; 
    }; 

    $content = DB::table($name)->select(...$data)->get(); 


    return view('back.group.view-table', compact('content', 'columns', 'data')); 
のようなものです

そして、私のブレードビューコードこれはトンであるこの

<table class="table table-striped"> 
      <thead> 
       <tr> 
        @foreach($columns as $column) 
         <th>{{ $column->dis_name }}</th> 
        @endforeach 
       </tr> 
      </thead> 
      <tbody> 
       @foreach ($content as $value) 
        <tr> 

         @for ($i = 0; $i < count($columns); $i++) 
          <td>{{ $value->key = $data[$i] }}</td> 
         @endfor 

        </tr> 
       @endforeach 

      </tbody> 
     </table> 

のようなものです彼は、私は次のコード This is the result wich i get with the following code

私はこの

<table class="table table-striped"> 
    <thead> 
     <tr> 
     @foreach($columns as $column) 
      <th>{{ $column->dis_name }}</th> 
     @endforeach 
     </tr> 
     </thead> 
     <tbody> 
     @foreach ($content as $value) 
     <tr> 
     <td>{{ $value->name }}</td> 
     </tr> 
     @endforeach 

    </tbody> 
</table> 
+0

するために、この

<table class="table table-striped"> <thead> <tr> @foreach($columns as $column) <th>{{ $column->dis_name }}</th> @endforeach </tr> </thead> <tbody> @foreach ($content as $value) <tr> @for ($i = 0; $i < count($columns); $i++) <td>{{ $value->key = $data[$i] }}</td> @endfor </tr> @endforeach </tbody> </table> 

から私の見解を変更する答えを見つけることが$ contentとして$ contentを入力してください{{ここにあなたの配列のインデックス名}} –

答えて

0

変更をしたい結果を得るウィッヒを引き起こすことarray_keys()と呼ばれる機能があります。

ので、それは私が

$results = DB::table($name)->select(...$data)->get(); 
$content = $results->first(); 
$keys = array_keys($content->toArray()); 

これはあなたのキーを取得すると思います。このようになります。各結果のキーが必要な場合は、$結果をループし、$結果ごとにarray_keys構文を使用してキーを取得します。

+0

私はそれを知っていますが、私はalredyがそれをしましたが、私はユーザーが自動的にユーザーが選択したテーブルから明瞭に理解されている場合 – Eth0

+0

foreachループはすでに同じことをしています...一度これをチェックしてください –

+0

テーブルユーザーが列idのユーザー名電子メールなどを持っている場合はどうですか私はdynamicllyなぜなら、彼は別の列を持つ別の表を選択するからです。製品ID名価格などそれはなぜ私がそれを動的にしたいのですか – Eth0

1

PHPのような The result i want to have

+0

も今あります[laravel収集方法単に 'keys'と呼ばれます)(https://laravel.com/docs/5.4/collections#method-keys) –

0

もう1つの解決策は、array_flip()を使用して、キーと値を反転させることです。配列からすべてのキーを取得する

0

使用array_keys()、あなたもforeach($arr as $key => $value)

0

を使用することができ、私は私がちょうどこの

<table class="table table-striped"> 
      <thead> 
       <tr> 
        @foreach($columns as $column) 
         <th>{{ $column->dis_name }}</th> 
        @endforeach 
       </tr> 
      </thead> 
      <tbody> 
       @foreach ($content as $value) 
        <tr> 

         @foreach ($data as $key) 
          <td>{{ $value->$key }}</td> 
         @endforeach 

        </tr> 
       @endforeach 

      </tbody> 
     </table> 
関連する問題