【问题标题】:How can I display dynamic key of array in the laravel blade?如何在 laravel 刀片中显示数组的动态键?
【发布时间】:2018-08-17 09:51:48
【问题描述】:

我的控制器是这样的:

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)) 中,结果如下:

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

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

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

所以数组数据就在那里

在这样的视图刀片 laravel 中:

<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>

它有效。但是我的数组键是动态的。密钥可以改变

所以数组可以这样改变:

#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 视图刀片中进行设置,使其可以适应动态数组?

更新

如果我echo '&lt;pre&gt;';print_r($data);'&lt;/pre&gt;';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
)

【问题讨论】:

  • 您是在问如何对数组进行排序?
  • @Classified No. 你尝试再次阅读我的问题
  • 你能展示你从哪里得到这个集合的控制器方法吗?
  • @yrv16 我更新了我的问题
  • 你需要有[['item_number' =&gt; ['name' ='th mane', 'value_1' =&gt; 'value_1'],...],...]这样的数组结构,它会帮助你解决你的任务。

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


【解决方案1】:

试试这个:

@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>

【讨论】:

  • 我希望表头也是动态的。所以在标签&lt;th&gt;中,它也是动态的。您的脚本不是动态的
  • 好的,让我看看
  • @SuccessMan 添加了动态标头代码。您可以在控制器中编写 PHP 代码并将 $key 也传递给查看。
  • 存在这样的错误:array_map(): Argument #2 should be an array
  • 查看我的更新问题。 $data数组的分页形式,也就是集合
【解决方案2】:

你的问题很混乱,如果 key 是动态的,你也可以发送一个带有“key”的数组,并且可以发送一个带有这样的键的数组

//In your controller
$values = array(
    array('item_number' => 'AB-0001','total_quantity' => '500', 'location-a' => '500'),
    array('item_number' => 'AB-0002','total_quantity' => '5000', 'location-a' => '5000'),
    array('item_number' => 'AB-0003','total_quantity' => '2000', 'location-a' => '2000'),
);

$data = 
[
    'keys' => ['item_number', 'total_quantity', 'location_a'],
    'values' => $values
];

return view('some_blade', $data);

在你的刀片中你可以这样访问

    @foreach($values as $value) 
     <tr>
       @foreach($keys as $k)
            <td>{{ $value[$k]; }}</td>
       @endforeach
    </tr>
    @endforeach

希望有帮助。

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2018-06-22
    • 1970-01-01
    • 2021-01-20
    • 2014-09-14
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多