2016-12-24 108 views
0

更改SKU列值我有一个对象:在收集地图

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场的第7个字符,而组我的收藏由SKU。

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

回答

0

你不能这样做,因为groupBy在sql级别上的集合工作,它会改变查询来得到你的结果。

你需要的是

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