コード例 #1
0
ファイル: ProductCtrl.php プロジェクト: udibagas/lara-shop
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(ProductRequest $request)
 {
     //
     $thumb = public_path('images/products/thumb');
     $full = public_path('images/products/full');
     try {
         $input = $request->except('image');
         $input['product_price'] = str_replace('.', '', $input['product_price']);
         $cat_slug = Category::find($input['id_category'])->slug;
         $input['slug'] = $cat_slug . '/' . str_slug($input['product_name']);
         $input['status'] = $request->get('status') == 'on' ? 1 : 0;
         $attr = array_filter($input['name']);
         $product = new Product($input);
         $product->save();
         $pro_id = $product->id;
         if (!empty($attr)) {
             Attribute::SaveAttribute($pro_id, $input['name'], $input['value']);
         }
         if ($request->hasFile('image')) {
             $images = $request->file('image');
             foreach ($images as $image) {
                 $name = str_random(5) . '.' . $image->getClientOriginalExtension();
                 $img = new Gambar();
                 $img->img_name = $name;
                 $img->id_product = $pro_id;
                 $img->path_thumb = 'images/products/thumb/' . $name;
                 $img->path_full = 'images/products/full/' . $name;
                 $img->save();
                 Image::make($image)->save($full . '/' . $name);
                 Image::make($image)->resize('100', '100')->save($thumb . '/' . $name);
             }
         }
     } catch (Exception $exc) {
         $message = $e->getMessage();
     }
     if (isset($message)) {
         return redirect()->back()->withInput()->withErrors(['message' => $message]);
     }
     return redirect()->route('backend.product.index');
 }
コード例 #2
0
ファイル: ImageCtrl.php プロジェクト: udibagas/lara-shop
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(ImageRequest $request)
 {
     //
     $thumb = public_path('images/products/thumb');
     $full = public_path('images/products/full');
     $input = $request->all();
     if ($request->hasFile('image')) {
         $images = $request->file('image');
         if ($request->has('id_product')) {
             $count = Gambar::where('id_product', '=', $request->get('id_product'))->count();
             if ($count >= 5) {
                 if ($request->ajax()) {
                     return response()->json(['error' => 'Maximum file gambar adalah 5.']);
                 }
                 return redirect()->back()->withErrors('Maximum file gambar adalah 5');
             }
         }
         foreach ($images as $image) {
             $name = str_random(5) . '.' . $image->getClientOriginalExtension();
             $img = new Gambar();
             $img->img_name = $name;
             if ($request->has('id_product')) {
                 $img->id_product = $input['id_product'];
             }
             $img->path_thumb = 'images/products/thumb/' . $name;
             $img->path_full = 'images/products/full/' . $name;
             $img->save();
             Image::make($image)->save($full . '/' . $name);
             Image::make($image)->resize('100', '100')->save($thumb . '/' . $name);
         }
     }
     if ($request->ajax()) {
         return response()->json(['success' => TRUE]);
     }
     return redirect()->back();
 }