61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* OrderProductRepo.php
|
|
*
|
|
* @copyright 2022 beikeshop.com - All Rights Reserved
|
|
* @link https://beikeshop.com
|
|
* @author Edward Yang <yangjin@guangda.work>
|
|
* @created 2022-07-04 21:14:12
|
|
* @modified 2022-07-04 21:14:12
|
|
*/
|
|
|
|
namespace Beike\Repositories;
|
|
|
|
use Beike\Models\Order;
|
|
use Beike\Models\OrderProduct;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class OrderProductRepo
|
|
{
|
|
/**
|
|
* 创建商品明细
|
|
*
|
|
* @param Order $order
|
|
* @param $cartProducts
|
|
*/
|
|
public static function createOrderProducts(Order $order, $cartProducts)
|
|
{
|
|
$orderProducts = [];
|
|
foreach ($cartProducts as $cartProduct) {
|
|
$productName = $cartProduct['name'];
|
|
$variantLabels = $cartProduct['variant_labels'] ?? '';
|
|
if ($variantLabels) {
|
|
$productName .= " - {$variantLabels}";
|
|
}
|
|
$orderProducts[] = [
|
|
'product_id' => $cartProduct['product_id'],
|
|
'order_number' => $order->number,
|
|
'product_sku' => $cartProduct['product_sku'],
|
|
'name' => $productName,
|
|
'image' => $cartProduct['image'],
|
|
'quantity' => $cartProduct['quantity'],
|
|
'price' => $cartProduct['price'],
|
|
];
|
|
}
|
|
$order->orderProducts()->createMany($orderProducts);
|
|
}
|
|
|
|
/**
|
|
* 查找单条商品明细数据
|
|
*
|
|
* @param $id
|
|
* @return Builder|Builder[]|Collection|Model|null
|
|
*/
|
|
public static function find($id): Model|Collection|Builder|array|null
|
|
{
|
|
return OrderProduct::query()->findOrFail($id);
|
|
}
|
|
}
|