|
@@ -65,8 +65,6 @@ class ProductController extends Controller
|
|
|
'price' => preg_replace('/[^0-9]+/', '', $request->price),
|
|
|
'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
|
|
|
]);
|
|
|
-
|
|
|
-
|
|
|
$data = [
|
|
|
'title' => $request->title,
|
|
|
'slug' => $request->slug,
|
|
@@ -78,37 +76,22 @@ class ProductController extends Controller
|
|
|
'discription' => $request->discription,
|
|
|
'creator_id' => auth()->user()->id
|
|
|
];
|
|
|
-
|
|
|
$product = Product::create($data);
|
|
|
+ $product->categories()->sync($request->categories);
|
|
|
|
|
|
- $file = $request->file('photo');
|
|
|
-// $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
|
|
|
-
|
|
|
- $fileMime = $file->getClientMimeType();
|
|
|
- $fileExtension = $file->getClientOriginalExtension();
|
|
|
- $fileName = 'product_file'. time() . '_' . $file->getClientOriginalName();
|
|
|
-// / $filePath = $file->storeAs('photos' . .$afterDiskRoot ,$fileName,'product');
|
|
|
|
|
|
- $filePath = $file->storeAs('photos', $fileName , 'product');
|
|
|
+ if ($request->has('photo')) {
|
|
|
+ $file = $request->file('photo');
|
|
|
+ $diskName = 'product';
|
|
|
+ $this->uploader($file, $diskName, $product);
|
|
|
|
|
|
- if (file_exists($filePath)) {
|
|
|
|
|
|
- $fileName = 'product_file'. time() . '_' . $fileName;
|
|
|
|
|
|
}
|
|
|
- $dataUpload = [
|
|
|
- 'name' => $fileName,
|
|
|
- 'extension' => $fileExtension,
|
|
|
- 'path' => $filePath,
|
|
|
- 'mime_type' => $fileMime,
|
|
|
- 'uploadable_type' => 'Packages\Product\Models\Product',
|
|
|
- 'uploadable_id' => $product->id,
|
|
|
|
|
|
- ];
|
|
|
+ $product->uploads()->sync($request->uploads);
|
|
|
|
|
|
|
|
|
- $product->categories()->sync($request->categories);
|
|
|
- $product->uploads()->create($dataUpload);
|
|
|
|
|
|
|
|
|
$msg = 'ذخیره محصول با موفقیت انجام شد ';
|
|
@@ -189,6 +172,46 @@ class ProductController extends Controller
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public function uploader($file, $diskName, $product)
|
|
|
+ {
|
|
|
+ $fileExtension = $file->getClientOriginalExtension();
|
|
|
+ $fileMimeType = $file->getMimeType();
|
|
|
+
|
|
|
+ $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
|
|
|
+
|
|
|
+ $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
|
|
|
+
|
|
|
+ $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
|
|
|
+
|
|
|
+ if (File_exists($filePath)) {
|
|
|
+ $fileName = time() . '_' . $fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
|
|
|
+
|
|
|
+ $uploadData = [
|
|
|
+ 'name' => $fileName,
|
|
|
+ 'path' => $upload,
|
|
|
+ 'mime_type' => $fileMimeType,
|
|
|
+ 'extension' => $fileExtension,
|
|
|
+
|
|
|
+ ];
|
|
|
+
|
|
|
+ $uploaded = $product->uploads()->create($uploadData);
|
|
|
+
|
|
|
+ }
|
|
|
+ public function serveFile(Request $request, $id)
|
|
|
+ {
|
|
|
+ $attachment = Upload::where('uploadable_type', 'Packages\Product\Models\Product')
|
|
|
+ ->where('id', $id)
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
+ if (!\Storage::disk('product')->exists($attachment->path))
|
|
|
+ abort(404);
|
|
|
+ $file_path = \Storage::disk('product')->getDriver()->getAdapter()->getPathPrefix() . $attachment->path;
|
|
|
+
|
|
|
+ return response()->file($file_path);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|