我有一个要从其中获取数组的类。
$attributeTypeOfProducts= $Attributetypes::with('cars')->get();我想使用array_pop方法删除数组的最后一条记录,但它给出了以下错误
Indirect modification of overloaded element of App\\Models\\Attributetypes has no effect我做了下面的
$pc=0;
$popcount =0;
for( $i = 0; $i < count($attributeTypeOfProducts); $i++){
for($pc=0; $pc<5; $pc++)
{
array_pop($attributeTypeOfProducts[$i]['cars']);
}
}
Response of the class
{
"id": 1,
"name": "cars",
"car_id": 123443221,
"status": 1,
"created_at": "2021-08-02T05:53:49.000000Z",
"updated_at": "2021-08-02T05:53:49.000000Z",
"cars": [
{
"id": 3,
"attr_size_value": 14,
"attr_image_src": "abc.png",
"attribute_size_name": "honda",
"attribute_type_id": 1,
"product_id": 123443221,
"created_at": "2021-08-02T05:53:50.000000Z",
"updated_at": "2021-08-02T05:53:50.000000Z"
},
{
"id": 4,
"attr_size_value": 10,
"attr_image_src": "abc.png",
"attribute_size_name": "bullet",
"attribute_type_id": 2,
"product_id": 123443221,
"created_at": "2021-08-02T05:54:28.000000Z",
"updated_at": "2021-08-02T05:54:28.000000Z"
},
{
"id": 8,
"attr_size_value": 12,
"attr_image_src": "abc.png",
"attribute_size_name": "hyundai",
"attribute_type_id": 3,
"product_id": 123443221,
"created_at": "2021-08-02T05:55:02.000000Z",
"updated_at": "2021-08-02T05:55:02.000000Z"
},
{
"id": 9,
"attr_size_value": 14,
"attr_image_src": "abc.png",
"attribute_size_name": "city",
"attribute_type_id": 3,
"product_id": 123443221,
"created_at": "2021-08-02T05:55:02.000000Z",
"updated_at": "2021-08-02T05:55:02.000000Z"
}
],
}我想删除带有attribute_size_name = city的记录,但它在array_pop上崩溃了
我有一个cars关系,我想删除它的最后一条记录,但它给出了前面提到的错误
执行此$Attributetypes::with('cars')->get()->toArray();操作后,我无法将另一个阵列推送到$attributeTypeOfProducts中
发布于 2021-08-02 11:39:05
您可以通过使用toArray()方法将集合转换为数组来实现您的结果:
$attributeTypeOfProducts= $Attributetypes::with('cars')->get()->toArray();您也可以使用laravel集合中的方法来完成此操作:
$attributeTypeOfProducts= $Attributetypes::with('cars')->get();
$attributeTypeOfProducts->map(function($item, $key){
if($key < 5){
array_pop($item['cars']);
return $item;
}
})https://stackoverflow.com/questions/68620549
复制相似问题