12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Distributor\Repositories;
- use App\Models\DistProductImage as Model;
- use Dcat\Admin\Repositories\EloquentRepository;
- class DistProductImage extends EloquentRepository
- {
-
- protected $eloquentClass = Model::class;
-
- 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,
- ];
- }
- }
-
- $i = 1;
- foreach ($images as $image) {
- $found = $existingImages->firstWhere('image_url', $image);
- if (!$found) {
- $result[] = [
- 'id' => 0,
- 'image_url' => $image,
- 'order' => $i,
- ];
- }
-
- foreach ($existingImages as $existingImage) {
- if ($existingImage->image_url == $image) {
- $existingImage->order = $i;
- $existingImage->save();
- }
- }
- $i++;
- }
- return $result;
- }
- }
|