0

我有一个循环遍历我的多数组,但我需要使用刀片语法设置每个条目的样式。我试过用 php 回显 Html,但没有奏效。

这就是我在 Blade 语法中尝试做的事情

 echo '<td>'.$key.'</td>';

大批

    array:2 [▼
  0 => {#173 ▼
    +"Name": "Rama Berger"
    +"StockName": "apple"
    +"price": 100
    +"Date": "2016-01-07 17:31:06"
  }
  1 => {#172 ▼
    +"Name": "Rama Berger"
    +"StockName": "apple"
    +"price": 11
    +"Date": "2016-01-07 20:00:38"
  }
]

看法

      <table class="table table-bordered table-hover table-striped">
           <thead>
               <tr>
                   <th>Name</th>
                   <th>Stock</th>
                   <th>Order Amount</th>
                   <th>Date</th><br>
               </tr>
           </thead>
           <tbody>
               @foreach($History as $Past)
                   @foreach($Past as $key)
                       <tr>
                           <td>{{$key}}</td>
                       </tr>
                   @endforeach
               @endforeach
           </tbody>
      </table>

控制器

<?php

namespace App\Http\Controllers;
use \View as View;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

 class RequestController extends BaseController
{
    public function GetHistory(){
        $name = Auth::user()->name;
        $History =  DB::table('History')->select('Name', 'StockName', 'price', 'Date')->where('StockName', '=', 'apple')->get();
        return view('pages.home')->with('History', $History);
    }
}
4

1 回答 1

0

您的问题有两种可能。

1) 检查您的刀片文件。应该是这样的filename.blade.php

2)你的阵列有问题。如果你的数组是对象数组,你应该像这样打印出来{{ $key->yourValue }}。否则,{{ $key['yourValue'] }}

如果您提供阵列样品,我们可以为您的问题提供更好的答案。

希望这有帮助!我想向你推荐一件事编码标准。

根据PSR标准,你应该遵循PSR标准。我不建议声明 like $History。应该是这样的$history。你可以从这里参考

改成这个

 @foreach($History as $Past)
      <tr>
          @foreach($Past as $key)
              <td>{{$key}}</td>
          @endforeach
      </tr>
 @endforeach
于 2016-01-08T04:09:07.883 回答