|
@@ -19,6 +19,7 @@ class ProductController extends Controller
|
|
|
{
|
|
|
|
|
|
}
|
|
|
+
|
|
|
// *******************************************show page product ************
|
|
|
public function index()
|
|
|
{
|
|
@@ -27,7 +28,7 @@ class ProductController extends Controller
|
|
|
$products = Product::query()->orderBy('id', 'DESC');
|
|
|
|
|
|
if (request()->has('productCategory') && request('productCategory') != '')
|
|
|
- $products = $products->whereHas('categories', function (Builder $query){
|
|
|
+ $products = $products->whereHas('categories', function (Builder $query) {
|
|
|
$query->whereRaw('id = ?', [request('productCategory')]);
|
|
|
});
|
|
|
|
|
@@ -57,7 +58,7 @@ class ProductController extends Controller
|
|
|
|
|
|
// *********************************************************save products ******************
|
|
|
|
|
|
- public function store(ProductRequest $request )
|
|
|
+ public function store(ProductRequest $request)
|
|
|
{
|
|
|
|
|
|
$request->merge([
|
|
@@ -79,50 +80,61 @@ class ProductController extends Controller
|
|
|
];
|
|
|
|
|
|
$product = Product::create($data);
|
|
|
- $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
|
|
|
- $diskName = 'product';
|
|
|
+
|
|
|
+ $file = $request->file('photo');
|
|
|
+// $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
|
|
|
+
|
|
|
$fileMime = $file->getClientMimeType();
|
|
|
$fileExtension = $file->getClientOriginalExtension();
|
|
|
- $fileName = 'product-file-' . '_' . $file->getClientOriginalName();
|
|
|
- $filePath = storage_path('app/product/' . $diskName . $afterDiskRoot . '/' . $fileName);
|
|
|
- if (File::exists($filePath)) {
|
|
|
+ $fileName = 'product_file'. time() . '_' . $file->getClientOriginalName();
|
|
|
+// / $filePath = $file->storeAs('photos' . .$afterDiskRoot ,$fileName,'product');
|
|
|
+
|
|
|
+ $filePath = $file->storeAs('photos', $fileName , 'product');
|
|
|
+
|
|
|
+ if (file_exists($filePath)) {
|
|
|
+
|
|
|
+ $fileName = 'product_file'. time() . '_' . $fileName;
|
|
|
|
|
|
- $fileName = time() . '_' . $fileName;
|
|
|
}
|
|
|
$dataUpload = [
|
|
|
- 'name' => $fileName,
|
|
|
- 'extension' => $fileExtension,
|
|
|
- 'path' => $filePath,
|
|
|
- 'mime_type' => $fileMime,
|
|
|
+ 'name' => $fileName,
|
|
|
+ 'extension' => $fileExtension,
|
|
|
+ 'path' => $filePath,
|
|
|
+ 'mime_type' => $fileMime,
|
|
|
+ 'uploadable_type' => 'Packages\Product\Models\Product',
|
|
|
+ 'uploadable_id' => $product->id,
|
|
|
|
|
|
];
|
|
|
- $product->upload->create($dataUpload);
|
|
|
+
|
|
|
|
|
|
$product->categories()->sync($request->categories);
|
|
|
- $product->upload()->sync($request->upload);
|
|
|
- dd($request->all());
|
|
|
+ $product->uploads()->create($dataUpload);
|
|
|
+
|
|
|
+
|
|
|
$msg = 'ذخیره محصول با موفقیت انجام شد ';
|
|
|
- return redirect(route('products.edit', $product->id), compact('upload'))->with('success', $msg);
|
|
|
+ return redirect(route('products.edit', $product->id))->with('success', $msg);
|
|
|
}
|
|
|
|
|
|
// ******************************************************edit page product*********************************************
|
|
|
|
|
|
public function edit(Product $product)
|
|
|
{
|
|
|
+
|
|
|
$categories = Category::orderBy('id', 'DESC')->get();
|
|
|
return view('product::product.edit', compact('product', 'categories'));
|
|
|
}
|
|
|
|
|
|
// *******************************************update page product*****************************************
|
|
|
|
|
|
- public function update(ProductRequest $request, Product $product)
|
|
|
+ public function update(ProductRequest $request, Product $product, Upload $upload)
|
|
|
{
|
|
|
+
|
|
|
$request->merge([
|
|
|
'price' => preg_replace('/[^0-9]+/', '', $request->price),
|
|
|
'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
|
|
|
]);
|
|
|
|
|
|
-// $data = array_merge($request->all(), ['creator_id' => auth()->user()->id]);
|
|
|
+
|
|
|
$data = [
|
|
|
'title' => $request->title,
|
|
|
'slug' => $request->slug,
|
|
@@ -136,11 +148,34 @@ class ProductController extends Controller
|
|
|
];
|
|
|
|
|
|
$product->update($data);
|
|
|
- $product->categories()->sync($request->categories);
|
|
|
|
|
|
- $msg = 'ویرایش محصول با موفقیت انجام شد ';
|
|
|
|
|
|
- return redirect(route('products.index'))->with('success', $msg);
|
|
|
+ $file = $request->file('photo');
|
|
|
+ $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate('m');
|
|
|
+ $diskName = 'product';
|
|
|
+ $fileMime = $file->getClientMimeType();
|
|
|
+ $fileExtension = $file->getClientOriginalExtension();
|
|
|
+ $fileName = 'product-file-' . '_' . $file->getClientOriginalName();
|
|
|
+ $filePath = $file->storeAs('photos', $fileName, 'product');
|
|
|
+ if (file_exists($filePath)) {
|
|
|
+
|
|
|
+ $fileName = time() . '_' . $fileName;
|
|
|
+
|
|
|
+ }
|
|
|
+ $dataUpload = [
|
|
|
+ 'name' => $fileName,
|
|
|
+ 'extension' => $fileExtension,
|
|
|
+ 'path' => $filePath,
|
|
|
+ 'mime_type' => $fileMime,
|
|
|
+ 'uploadable_type' => 'Packages\Product\Models\Product',
|
|
|
+ 'uploadable_id' => $product->id,
|
|
|
+
|
|
|
+ ];
|
|
|
+ $product->categories()->sync($request->categories);
|
|
|
+ $product->uploads()->update($dataUpload);
|
|
|
+ $msg = 'ویرایش محصول با موفقیت انجام شد ';
|
|
|
+ dd($request->all());
|
|
|
+ return redirect(route('products.index'),compact('upload'))->with('success', $msg);
|
|
|
}
|
|
|
|
|
|
//***************************** delete products*****************************************
|
|
@@ -155,4 +190,5 @@ class ProductController extends Controller
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
}
|