使用地图更改集合中的SKU列值

时间:2016-12-24 13:22:30

标签: laravel dictionary collections

我有一个对象:

array:100 [▼
0 => array:62 [▼
"id" => 9407
"name" => "Gramophone (El)"
"slug" => "gramophone-el"
"permalink" => "https://wordpress.dev/index.php/produit/gramophone-el/"
"date_created" => "2016-12-23T16:57:13"
"date_modified" => "2016-12-23T23:10:40"
"type" => "variable"
"status" => "publish"
"featured" => false
"catalog_visibility" => "visible"
"description" => "<p>Des magistrats au tribunal écoutent parler un Gramophone en cours d'audience.</p>\n"
"short_description" => ""
"sku" => 1900123Z0
"price" => ""
"regular_price" => ""
"sale_price" => ""
"date_on_sale_from" => ""
"date_on_sale_to" => ""
"price_html" => ""
"on_sale" => false
"purchasable" => false
"total_sales" => 0
"virtual" => false
"downloadable" => false
"downloads" => []
"download_limit" => -1
"download_expiry" => -1
"download_type" => "standard"
"external_url" => ""
"button_text" => ""
"tax_status" => "taxable"
"tax_class" => ""
"manage_stock" => false
"stock_quantity" => null
"in_stock" => false
"backorders" => "no"
"backorders_allowed" => false
"backordered" => false
"sold_individually" => false
"weight" => ""
"dimensions" => array:3 [ …3]
"shipping_required" => true
"shipping_taxable" => true
"shipping_class" => ""
"shipping_class_id" => 0
"reviews_allowed" => true
"average_rating" => "0.00"
"rating_count" => 0
"related_ids" => array:5 [ …5]
"upsell_ids" => []
"cross_sell_ids" => []
"parent_id" => 0
"purchase_note" => ""
"categories" => array:1 [ …1]
"tags" => array:1 [ …1]
"images" => array:1 [ …1]
"attributes" => array:7 [ …7]
"default_attributes" => []
"variations" => []
"grouped_products" => []
"menu_order" => 0
"_links" => array:2 [ …2]

    $products->map(function($item){
        return $item['sku'] = substr($item['sku'], 0, 7);

    })->groupBy('sku');

我需要的是保留SKU Field的前7个字符,并按SKU对我的收藏进行分组。

事情就像我一样,sku永远不会被修改....我的错误是什么????

1 个答案:

答案 0 :(得分:0)

你不能这样做,因为groupBy收集工作在sql级别,它会改变查询以获得结果。

您需要的是

$groupByProducts = $products->groupBy(DB::raw('substr(sku, 0, 7)'));