laravel - 如何在 Laravel Blade 中显示数组的动态键?

标签 laravel loops laravel-5 laravel-5.6 laravel-blade

我的 Controller 是这样的:

public function index(Request $request)
{
    $param = $request->all();
    $data = $this->itemDetailRepository->getData($param);
    return view('item-detail.index', compact('param'))
        ->withItems(count($data)>0?$data:null);
}

如果我调试$data或者在 laravel ( dd($data) ) 中,结果如下:

enter image description here

所以数组$data集合和分页是这样的吗

如果我单击属性部分,它会显示如下:

#attributes: array:5 [▼
  "item_number" => "AB-0001"
  "total_quantity" => "1000"
  "location-a" => "500"
  "location-b" => "500"
]

所以数组数据就在那里

在 Laravel View Blade 中,如下所示:

<table>
    <tr>
        <th>ITEM NUMBER</th>
        <th>TOTAL QUANTITY</th>
        <th>LOCATION-A</th>
        <th>LOCATION-B</th>
    </tr>
        @foreach($items as $item)
        <tr>
            <td>{{ $item->item_number }}</td>
            <td>{{ $item->total_quantity }}</td>
            <td>{{ $item['location-a'] }}</td>
            <td>{{ $item['location-b'] }}</td>
        </tr>
        @endforeach
</table>

它有效。但我的数组键是动态的。 key 可以更改

所以数组可以这样改变:

#attributes: array:5 [▼
  "item_number" => "AB-0001"
  "total_quantity" => "500"
  "location-a" => "500"
]

或者可以像这样改变:

#attributes: array:5 [▼
  "item_number" => "AB-0001"
  "total_quantity" => "1500"
  "location-a" => "500"
  "location-b" => "500"
  "location-c" => "500"
]

所以位置是动态的

如何在 laravel View Blade 中设置它,以便它可以调整为动态数组?

更新

如果我echo '<pre>';print_r($data);'</pre>';die(); ,结果如下:

Illuminate\Pagination\LengthAwarePaginator Object
(
    [total:protected] => 2428
    [lastPage:protected] => 1214
    [items:protected] => Illuminate\Database\Eloquent\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => App\Models\ItemDetail Object
                        (
                            [table:protected] => items_details
                            [fillable:protected] => Array
                                (
                                    [0] => item_number
                                    [1] => quantity
                                    ...
                                )

                            [connection:protected] => mysql
                            [primaryKey:protected] => id
                            [keyType:protected] => int
                            [incrementing] => 1
                            [with:protected] => Array
                                (
                                )

                            [withCount:protected] => Array
                                (
                                )

                            [perPage:protected] => 15
                            [exists] => 1
                            [wasRecentlyCreated] => 
                            [attributes:protected] => Array
                                (
                                    [item_number] => AB-0001
                                    [total_quantity] => 1000
                                    [location-a] => 500
                                    [location-b] => 500
                                )

                            [original:protected] => Array
                                (
                                    [item_number] => AB-0001
                                    [total_quantity] => 1000
                                    [location-a] => 500
                                    [location-b] => 500
                                )

                            [changes:protected] => Array
                                (
                                )

                            [casts:protected] => Array
                                (
                                )

                            [dates:protected] => Array
                                (
                                )

                            [dateFormat:protected] => 
                            [appends:protected] => Array
                                (
                                )

                            [dispatchesEvents:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [relations:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [timestamps] => 1
                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                        )

                    [1] => App\Models\ItemDetail Object
                        (
                            [table:protected] => items_details
                            [fillable:protected] => Array
                                (
                                    [0] => item_number
                                    [1] => quantity
                                    ...
                                )

                            [connection:protected] => mysql
                            [primaryKey:protected] => id
                            [keyType:protected] => int
                            [incrementing] => 1
                            [with:protected] => Array
                                (
                                )

                            [withCount:protected] => Array
                                (
                                )

                            [perPage:protected] => 15
                            [exists] => 1
                            [wasRecentlyCreated] => 
                            [attributes:protected] => Array
                                (
                                    [item_number] => AB-0002
                                    [total_quantity] => 1500
                                    [location-a] => 1000
                                    [location-b] => 500
                                )

                            [original:protected] => Array
                                (
                                    [item_number] => AB-0002
                                    [total_quantity] => 1500
                                    [location-a] => 1000
                                    [location-b] => 500
                                )


                            [changes:protected] => Array
                                (
                                )

                            [casts:protected] => Array
                                (
                                )

                            [dates:protected] => Array
                                (
                                )

                            [dateFormat:protected] => 
                            [appends:protected] => Array
                                (
                                )

                            [dispatchesEvents:protected] => Array
                                (
                                )

                            [observables:protected] => Array
                                (
                                )

                            [relations:protected] => Array
                                (
                                )

                            [touches:protected] => Array
                                (
                                )

                            [timestamps] => 1
                            [hidden:protected] => Array
                                (
                                )

                            [visible:protected] => Array
                                (
                                )

                            [guarded:protected] => Array
                                (
                                    [0] => *
                                )

                        )

                )

        )

    [perPage:protected] => 2
    [currentPage:protected] => 1
    [path:protected] => http://my-project.test/admin/item-detail
    [query:protected] => Array
        (
        )

    [fragment:protected] => 
    [pageName:protected] => page
)

最佳答案

试试这个:

@php
$counts = array_map('count', $data);
$key = array_flip($counts)[max($counts)];
@endphp
<table>
    <tr>
        @foreach ($data[$key] as $key => $value)
            <th>{{$key}}</th>
        @endforeach
    </tr>
        @foreach($data as $value)
        <tr>
            @foreach ($value as $key => $value)
                <td>{{$value}}</td>
            @endforeach
        </tr>
        @endforeach
</table>

关于laravel - 如何在 Laravel Blade 中显示数组的动态键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51892771/

相关文章:

laravel - 有没有办法将Docker容器上设置的系统环境变量传递给Laravel-5.6应用

php - Laravel,使用模型方法作为属性

php - 在 Laravel 中创建搜索工具

ios - 快速更新循环

database - Grammar.php 第 39 行中的 ErrorException : Array to string conversion

php - laravel 5.2 重定向消息

php - artisan 迁移找不到驱动程序

php - 文本对齐 : center on Generate PDF via DomPDF (Laravel) a little bit offset to the right

loops - 迭代向量的相邻元素时如何避免循环

python - 如何从 URL 列表中获取属性 ('innerHTML' ) - Selenium?