Advertisement
Guest User

product.php

a guest
Feb 22nd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.44 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Models\Product;
  7. use App\Models\Tipe;
  8. use App\Models\Category;
  9. use App\Models\Size;
  10. use App\Models\Color;
  11. use App\Models\Image as ImageModel;
  12. use App\Models\Spesifikasi;
  13. use Intervention\Image\Facades\Image as Image;
  14. use DB;
  15.  
  16. class ProdukController extends Controller
  17. {
  18.     public function index()
  19.     {
  20.       $produks = Product::with(['categories'])->orderBy('created_at','asc')->get();
  21.       $tipes = Tipe::all();
  22.       $sizes = Size::all();
  23.       $warnas = Color::all();
  24.       $spesification = Spesifikasi::all();
  25.       $categories = Category::all();
  26.       return View('admin.produk', ['produks' => $produks,
  27.                                    'tipes' => $tipes,
  28.                                    'categories' => $categories,
  29.                                    'sizes' => $sizes,
  30.                                    'warnas' => $warnas,
  31.                                    'spesification' => $spesification,
  32.                                  ]);
  33.     }
  34.  
  35.     public function store(Request $request)
  36.     {
  37.         // dd($war = explode(',', $request->kategori));
  38.         $product = new Product;
  39.         $product->kd_product   = $request->kd_produk;
  40.         $product->title        = $request->title;
  41.         $product->restock      = date("Y-m-d", strtotime($request->restock));
  42.         $product->harga        = $request->harga;
  43.         $product->link         = $request->link;
  44.         $product->tipe         = $request->tipe;
  45.         $product->status       = "unpublish";
  46.         $product->jml_produksi = $request->jml_produksi;
  47.         $product->jml_klik     = 0;
  48.  
  49.         $product->save();
  50.         $pid = $product->id;
  51.  
  52.         //insert kategori
  53.         $kat = explode(',', $request->kategori);
  54.         $category = Category::find($kat);
  55.         $product->categories()->attach($category);
  56.  
  57.         //insert color
  58.         $war = explode(',', $request->warna);
  59.         $color = Color::find($war);
  60.         $product->colors()->attach($color);
  61.  
  62.         //insert spesifikasi
  63.         $spec = explode(',', $request->spesifikasi);
  64.         $spesifikasi = Spesifikasi::find($spec);
  65.         $product->spesifikasis()->attach($spesifikasi);
  66.  
  67.         //insert ukuran
  68.         $siz = explode(',', $request->ukuran);
  69.         $size = Size::find($siz);
  70.         $product->sizes()->attach($size);
  71.  
  72.         $images = [];
  73.         foreach(json_decode($request->gambar) as $i) {
  74.           $fileName = time() . '_' . date('ymdhis') . '_' . uniqid();
  75.           Image::make($i->image)
  76.               ->save(public_path('storage/produk/').$fileName.'.png');
  77.  
  78.           $images[] = [
  79.             'product_id' => $pid,
  80.             'gambar' => $fileName.'.png',
  81.             'thumbnail' => $fileName.'.png',
  82.           ];
  83.         }
  84.  
  85.         ImageModel::insert($images);
  86.  
  87.  
  88.         return response()->json($product);
  89.       // dd($request->tipe);
  90.       // dd($request->kategori);
  91.       // $validator = Validator::make(Input::all(), $this->rules);
  92.       //  if ($validator->fails()) {
  93.       //      return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
  94.       //  } else {
  95.       //      $tipe = new Tipe();
  96.       //      $tipe->tipe = $request->tipe;
  97.       //      $tipe->save();
  98.       //      return response()->json($tipe);
  99.        // }
  100.     }
  101.  
  102.     public function update(Request $request, $id)
  103.     {
  104.       DB::beginTransaction();
  105.       try {
  106.         $product = Product::findOrFail($id);
  107.         $product->title = $request->produk;
  108.         $product->save();
  109.  
  110.         $images = [];
  111.  
  112.         ImageModel::where('product_id', $id)->delete();
  113.  
  114.         foreach(json_decode($request->gambar) as $i) {
  115.           $fileName = time() . '_' . date('ymdhis') . '_' . uniqid();
  116.           Image::make($i->image)
  117.               ->save(public_path('storage/produk/').$fileName.'.png');
  118.  
  119.           $images[] = [
  120.             'product_id' => $id,
  121.             'gambar' => $fileName.'.png',
  122.             'thumbnail' => $fileName.'.png',
  123.           ];
  124.         }
  125.  
  126.         ImageModel::insert($images);        
  127.         DB::commit();
  128.         return response()->json(json_decode(Product::whereId($id)->with('images')->first()));
  129.       } catch (\Exception $e) {
  130.         DB::rollback();
  131.       }
  132.     }
  133.  
  134.     public function show(Request $request, $id)
  135.     {
  136.       $product = Product::whereId($id)->with('images')->first();
  137.       return response()->json($product);
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement