PATH:
home
/
lab2454c
/
.trash
/
core
/
app
/
Http
/
Controllers
<?php namespace App\Http\Controllers; use App\Models\Brand; use App\Models\Category; use App\Models\Subcategory; use App\Models\Order; use App\Models\Product; use App\Models\ProductSpecification; use App\Models\Specification; use App\Rules\FileTypeValidate; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Models\ProductImage; class ProductController extends Controller { public function __construct() { $this->activeTemplate = activeTemplate(); } public function index() { $user = Auth::user(); $pageTitle = "Manage Product"; $emptyMessage = "No data found"; $products = Product::where('user_id', $user->id)->latest()->with('category', 'productSpecification')->paginate(getPaginate()); return view($this->activeTemplate. 'user.product.index', compact('pageTitle', 'emptyMessage', 'products')); } public function create() { $pageTitle = "Create Product"; $brands = Brand::where('status', 1)->select('id', 'name')->get(); return view($this->activeTemplate . 'user.product.create', compact('pageTitle', 'brands')); } public function store(Request $request) { $request->validate([ 'featured'=> 'nullable|in:1', 'title'=> 'required|max:255', 'sub_title'=> 'required|max:450', 'amount'=> 'required|numeric|gt:0', 'keyword' => 'nullable|array|min:1|max:15', 'time_duration'=> 'required|before_or_equal:today', // 'category_id'=> 'required|exists:categories,id', // 'sub_category'=> 'required|exists:subcategories,id', //'brand'=> 'required|exists:brands,id', 'description' => 'required', //'image'=> ['required','image',new FileTypeValidate(['jpeg', 'jpg', 'png'])], 'image'=>'required|array', 'image.*'=> 'file|mimes:jpeg,png,jpg,svg,gif,mp4,mov|max:10000', 'package' => 'nullable|in:1,0' ]); $category = Auth::user()->category; $subCategory = Auth::user()->subcategory; //$brand = Brand::where('id', $request->brand)->where('status', 1)->firstOrFail(); $user = Auth::user(); $product = new Product(); $product->user_id = $user->id; $product->title = $request->title; $product->sub_title = $request->sub_title; $product->amount = $request->amount; $product->time_duration = $request->time_duration; $product->description = $request->description; $product->featured = $request->featured ? $request->featured : null; $product->package = $request->package ? $request->package : 0; $product->keyword = $request->keyword; $product->category()->associate($category); $product->subCategory()->associate($subCategory); //$product->brand()->associate($brand); $product->save(); // if ($request->hasFile('image')) { // $path = imagePath()['product']['path']; // $size = imagePath()['product']['size']; // try { // $filename = uploadImage($request->image, $path, $size); // $product->image = $filename; // } catch (\Exception $exp) { // $notify[] = ['errors', 'Image could not be uploaded.']; // return back()->withNotify($notify); // } // } if ($request->hasFile('image')) { foreach ($request->file('image') as $image) { $path = imagePath()['product']['path']; $size = imagePath()['product']['size']; $extension = $image->getClientOriginalExtension(); if ($extension == 'mp4' || $extension == 'mov') { $filename = uploadFile($image, $path, $size); } else{ $filename = uploadImage($image, $path, $size); } //$filename = uploadImage($image, $path, $size); $productImage = new ProductImage(); $productImage->file = $filename; $productImage->product()->associate($product); $productImage->save(); } } $notify[] = ['success', 'Product has been created']; return redirect()->route('user.product.index')->withNotify($notify); } public function edit($id) { $user = Auth::user(); $pageTitle = "Product Update"; $emptyMessage = "No data found"; $brands = Brand::where('status', 1)->select('id', 'name')->get(); $product = Product::where('user_id', $user->id)->where('id', $id)->firstOrFail(); return view($this->activeTemplate . 'user.product.edit', compact('pageTitle', 'emptyMessage','product','brands')); } public function update(Request $request, Product $product) { $changed = false; $values = $request->validate([ 'featured'=> 'nullable|in:1', 'title'=> 'required|max:255', 'sub_title'=> 'required|max:450', 'amount'=> 'required|numeric|gt:0', 'keyword' => 'nullable|array|min:1|max:15', 'time_duration'=> 'required|before_or_equal:today', 'description' => 'required', // 'category_id'=> 'required|exists:categories,id', // 'sub_category'=> 'required|exists:subcategories,id', //'brand'=> 'required|exists:brands,id', 'image'=>'nullable|array', 'image.*'=> 'file|mimes:jpeg,png,jpg,svg,gif,mp4,mov|max:10000', 'package' => 'nullable|in:1,0' ]); // $category = Category::where('id', $values['category_id'])->where('status', 1)->firstOrFail(); // $subCategory = Subcategory::where('id', $request->sub_category)->firstOrFail(); //$brand = Brand::where('id', $values['brand'])->where('status', 1)->firstOrFail(); $product->fill($request->only('featured', 'title','sub_title','amount','keyword','time_duration','description','package')); if ($product->isDirty()) { $changed = true; $product->save(); } // if ($category != $product->category) { // $product->category()->associate($category); // $product->save(); // $changed = true; // } // if ($subCategory != $product->subCategory) { // $product->subCategory()->associate($subCategory); // $product->save(); // $changed = true; // } // if ($brand != $product->brand) { // $product->brand()->associate($brand); // $product->save(); // $changed = true; // } /* Code Start for Multiple image upload */ $imageIds = collect($request->image_id); //dd($imageIds); /* Check If User remove images */ foreach($product->productImages as $image){ if(!$imageIds->contains($image->id)){ /* Delete Existing file from Folder */ if(\File::exists(base_path('../assets/images/product/'.$image->file))){ \File::delete(base_path('../assets/images/product/'.$image->file)); } $image->delete(); } } foreach($imageIds as $i => $id) { /* Check If User add images while update */ if($id == 0) { $path = imagePath()['product']['path']; $size = imagePath()['product']['size']; //$filename = uploadImage($values['image'][$i], $path, $size); $extension = $request->image[$i]->getClientOriginalExtension(); if ($extension == 'mp4' || $extension == 'mov') { $filename = uploadFile($values['image'][$i], $path, $size); } else{ $filename = uploadImage($values['image'][$i], $path, $size); } $productImage = new ProductImage(); $productImage->file = $filename; $productImage->product()->associate($product); $productImage->save(); $changed = true; } /* Check If User update existing images while update */ else { if(isset($values['image'][$i])) { $path = imagePath()['product']['path']; $size = imagePath()['product']['size']; //$filename = uploadImage($values['image'][$i], $path, $size); $extension = $request->image[$i]->getClientOriginalExtension(); if ($extension == 'mp4' || $extension == 'mov') { $filename = uploadFile($values['image'][$i], $path, $size); } else{ $filename = uploadImage($values['image'][$i], $path, $size); } $productImage = $product->productImages->where('id', "=", $id)->first(); /* Delete Existing file from Folder */ if(\File::exists(base_path('../assets/images/product/'.$productImage->file))){ \File::delete(base_path('../assets/images/product/'.$productImage->file)); } $productImage->file = $filename; $productImage->product()->associate($product); $productImage->save(); $changed = true; } } } /* Code End for Multiple image upload */ if (! $changed) { $notify[] = ['warning', 'No changes done to save']; return redirect()->route('user.product.index')->withNotify($notify); } $notify[] = ['success', 'Product has been Updated']; return redirect()->route('user.product.index')->withNotify($notify); } public function addSpecification($id) { $user = Auth::user(); $pageTitle = "Specification"; $emptyMessage = "No data found"; $product = Product::where('id',$id)->where('user_id', $user->id)->firstOrFail(); $specifications = Specification::where('category_id', $product->category_id)->with('productSpecification')->get(); return view($this->activeTemplate . 'user.product.specification', compact('pageTitle', 'emptyMessage', 'product', 'specifications')); } public function storeSpecification(Request $request, $id) { $i=0;$j=0; $user = Auth::user(); $product = Product::where('user_id', $user->id)->where('id', $id)->firstOrFail(); if($product->status == 1){ $notify[] = ['error', "Can't update approved product"]; return back()->withNotify($notify); } $specifications = Specification::where('category_id', $product->category_id)->pluck('slug',)->toArray(); $specificationId = Specification::where('category_id', $product->category_id)->pluck('id',)->toArray(); $requestData = $request->except('_token'); foreach($requestData as $value){ $request->validate([ $specifications[$i] => 'required|max:120' ]); $i++; } $productSpecificationDelete = ProductSpecification::where('product_id', $product->id)->delete(); foreach($requestData as $value){ $productSpecification = new ProductSpecification(); $productSpecification->product_id = $product->id; $productSpecification->specification_id = $specificationId[$j]; $productSpecification->value = $value; $productSpecification->save(); $j++; } $notify[] = ['success', 'Product specification has been created']; return back()->withNotify($notify); } }
[+]
..
[-] CollectionController.php
[edit]
[-] UserController.php
[edit]
[+]
Api
[+]
Gateway
[-] OrderProductController.php
[edit]
[-] ContactController.php
[edit]
[-] Controller.php
[edit]
[+]
Admin
[-] OrderController.php
[edit]
[+]
Auth
[-] ReviewController.php
[edit]
[-] PurchaseProductController.php
[edit]
[-] SiteController.php
[edit]
[-] AuthorizationController.php
[edit]
[-] FashionNftController.php
[edit]
[-] ProductController.php
[edit]
[-] ShowcaseController.php
[edit]
[-] TicketController.php
[edit]
[-] CronController.php
[edit]