12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Admin\Repositories;
- use App\Models\BaseProductImage as Model;
- use Dcat\Admin\Repositories\EloquentRepository;
- use Carbon\Carbon;
- class BaseProductImage extends EloquentRepository
- {
-
- protected $eloquentClass = Model::class;
- public static function deleteByProductId($productId)
- {
- Model::where('product_id', $productId)->delete();
- }
- public static function saveProductImages($productId, $imageUrls)
- {
- if (empty($productId) || empty($imageUrl)) {
- return false;
- }
-
- $data = [];
- foreach ($imageUrls as $imageUrl) {
- $data[] = [
- 'product_id' => $productId,
- 'image_url' => $imageUrl,
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- ];
- }
-
- return Model::insert($data);
- }
-
- public static function formatData($productId, $images)
- {
- $existingImages = Model::where('product_id', $productId)->get();
-
- $result = [];
-
- foreach ($existingImages as $existingImage) {
- if (!in_array($existingImage->image_url, $images)) {
- $result[] = [
- 'id' => $existingImage->id,
- 'image_url' => $existingImage->image_url,
- '_remove_' => 1,
- ];
- }
- }
-
- foreach ($images as $image) {
- $found = $existingImages->firstWhere('image_url', $image);
- if (!$found) {
- $result[] = [
- 'id' => 0,
- 'image_url' => $image,
- ];
- }
- }
- return $result;
- }
- }
|