【问题标题】:Laravel Format Array Data to TableLaravel 将数组数据格式化为表格
【发布时间】:2021-07-07 16:30:35
【问题描述】:

我正在尝试在我的刀片模板中创建一个表。当我遍历数组一次时,我收到以下错误htmlspecialchars() expects parameter 1 to be string, array given 。

当我循环两次时,表格格式不正确。

我的刀片模板如下所示:

@foreach($checkins as $index=>$opt)
          @foreach($checkins as $index=>$opt)
             @foreach($opt as $key => $value)
               <tr> {{$key}} </tr>
               <td> {{$value}}  </td>    
             @endforeach    
                                                        
         @endforeach                                        

我的控制器看起来像这样,我使用 getAddress 函数将纬度和经度转换为实际位置:

if($request->has('search')) {
            $checkins = Checkin::with(['user'])->where('name', 'like', '%'.$request->search.'%')->paginate(setting('record_per_page', 15));
        } else {
            $checkins = Checkin::with(['user'])->get();
        }

        $title = "Manage Checkins";

        
        $checkins = $checkins->map(function ($checkin) {

            $location = $this->getAddress($checkin->lat,$checkin->long);

            return [
                'location' => $location,
                'student' => $checkin->user->first_name. ' ' .$checkin->user->first_name,
                'supervisor' => $checkin->supervisor === null ? '' : $checkin->supervisor->first_name. ' ' .$checkin->supervisor->last_name  ,
                'created_at' => $checkin->created_at,
            ];
        });

        return view('checkins.index', compact('checkins', 'title'));

如下所示的数组:

[
{
location: "Government of Southern Sudan Nairobi Liason Office, 5th Avenue Ngong, Hurlingham, Nairobi, P. O. BOX 41362, Kenya",
student: "Super Admin Super Admin",
supervisor: "Super Admin Admin",
created_at: null
},
{
location: "Kajiado, Kenya",
student: "Test Test",
supervisor: "Super Admin Admin",
created_at: "2021-07-06T12:13:54.000000Z"
},
{
location: "Murang`a, Central Kenya, Kenya",
student: "Test Test",
supervisor: "Super Admin Admin",
created_at: "2021-07-06T12:16:19.000000Z"
}
]

任何关于我如何将键显示为值和键下方的实际值的帮助/链接将不胜感激。

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    第一个循环返回关联数组的每个单独项,第二个值循环每个关联数组到键值对

         @foreach($checkins as $key=>$value)
               @foreach($value as $iKey=>$iValue)
               <tr>          
                  <td> {{$iKey}} </td>
                  <td> {{$iValue}}  </td>    
               </tr>
                                                                        
            @endforeach  
     @endforeach  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多