* @created 2022-07-01 11:15:25 * @modified 2022-07-01 11:15:25 */ namespace Beike\Admin\Services; use Beike\Repositories\CustomerGroupRepo; class CustomerGroupService { /** * @param $data * @return int */ public static function create($data) { $customerGroup = CustomerGroupRepo::create($data); $descriptions = []; foreach ($data['descriptions'] as $locale => $description) { $description['locale'] = $locale; $descriptions[] = $description; } $customerGroup->descriptions()->createMany($descriptions); return $customerGroup; } public static function update($id, $data) { $customerGroup = CustomerGroupRepo::find($id); if (!$customerGroup) { throw New \Exception("您更新的ID不存在"); } $customerGroup->update($data); $customerGroup->descriptions()->delete(); $descriptions = []; foreach ($data['descriptions'] as $locale => $description) { $description['locale'] = $locale; $descriptions[] = $description; } $customerGroup->descriptions()->createMany($descriptions); return $customerGroup; } }