Files
getgreen-backend/app/Jobs/Dashboard/Product/Child.php
2026-04-28 16:00:18 +05:00

78 lines
2.2 KiB
PHP
Executable File

<?php
namespace App\Jobs\Dashboard\Product;
use App\Models\Product;
use App\Models\Screen;
use App\Support\Uploads;
use Carbon\Carbon;
use App\Api\ImageResize;
use App\Http\Requests\Dashboard\Product\Store as StoreRequest;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Storage;
class Child
{
protected $request;
protected $image;
protected $product;
protected $size;
/**
* Child constructor.
* @param StoreRequest $request
* @param Product $product
*/
public function __construct(StoreRequest $request, Product $product)
{
$this->request = $request;
$this->product = $product;
$this->image = new ImageResize();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
foreach ($this->request->colors as $color) {
$color_id = $color['color_id'] == "null" ? null : $color['color_id'];
$sizes = !empty($color['sizes']) ? $color['sizes'] : null;
$child = Product::create([
'color_id' => $color_id,
'sizes' => $sizes,
'article_number' => $color['article_number'],
'child_id' => $this->product->id,
'published' => $this->product->published,
'available' => true
]);
if (!empty($color['screens'])) {
foreach ($color['screens'] as $screen) {
$folder = Carbon::now()->format('Y/m/d');
// Store original
$path = Uploads::store($screen['image'], "uploads/screens/{$folder}");
// Store and resize thumb
$tempPath = Uploads::store($screen['image'], 'temp', 'public');
$thumbPath = $this->image->resize($tempPath, 322, 'screens', true);
$this->size = Storage::size($path);
Screen::create([
'path' => $path,
'path_thumb' => $thumbPath,
'name' => basename($path),
'product_id' => $child->id,
'size' => $this->size
]);
}
}
}
}
}