Advertisement
ronikuchan

temporary

Dec 19th, 2020
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. public function storeAdmin(Request $request)
  2.     {
  3.         $validator = Validator::make($request->all(), [
  4.             'name' => 'required|max:191',
  5.             'stok' => 'required|numeric',
  6.             'berat_kemasan' => 'required|numeric',
  7.             'deskripsi' => 'required',
  8.             'foto_1' => 'required|image',
  9.             'type' => 'in:distributor,mitra',
  10.             'kategori_id' => 'required',
  11.             'satuan' => 'required',
  12.         ]);
  13.         if ($validator->fails()) {
  14.             return response()->json([
  15.                 'success' => false,
  16.                 'messages' => 'Please fill in the blank !',
  17.                 'data' => $validator->errors(),
  18.             ], 403);
  19.         }
  20.         $sizeFotoOne = $_FILES['foto_1']['size'];
  21.         if ($sizeFotoOne > 1048576) {
  22.             $msg = "Ukuran terlalu besar, maksima 1 MB (file". $sizeFotoOne . ")";
  23.             return errorMsg($msg);
  24.         }
  25.  
  26.         $foto1 = rand(1, 199) . $_FILES['foto_1']['name'];
  27.  
  28.         $userID = Auth::id();
  29.  
  30.         $Produk = Produk::create([
  31.             'name' => $request->input('name'),
  32.             'stok' => (int) $request->input('stok'),
  33.             'berat_kemasan' => (double) $request->input('berat_kemasan'),
  34.             'harga_supplyer' => (int) $request->input('harga_supplyer'),
  35.             'harga_jual' => (int) $request->input('harga_jual'),
  36.             'harga_promo' => (int) $request->input('harga_promo'),
  37.             'expire' => $request->input('expire'),
  38.             'deskripsi' => $request->input('deskripsi'),
  39.             'foto_1' => $foto1,
  40.             'foto_2' => $foto1,
  41.             'foto_3' => $foto1,
  42.             'type' => $request->input('type'),
  43.             'user_id' => $userID,
  44.             'kategori_id' => $request->input('kategori_id'),
  45.             'satuan' => $request->input('satuan'),
  46.             'preorder' => $request->input('preorder'),
  47.             'preorder_notes' => $request->input('preorder_notes'),
  48.             'cogs' => (int) $request->input('cogs'),
  49.             'diskon' => (int) $request->input('diskon'),
  50.             'margin_pusat' => (int) $request->input('margin_pusat'),
  51.         ]);
  52.  
  53.         $tempDir = storage_path("temp/");
  54.         move_uploaded_file($_FILES["foto_1"]["tmp_name"], $tempDir . $foto1);
  55.         Storage::disk('produk')->putFileAs($Produk->id, new File($tempDir . $foto1), $foto1);
  56.  
  57.         if ($Produk) {
  58.             return response()->json([
  59.                 'success' => true,
  60.                 'messages' => 'Add Produk Success !',
  61.                 'data' => $Produk,
  62.             ], 201);
  63.         } else {
  64.             return response()->json([
  65.                 'success' => false,
  66.                 'messages' => 'Can\'t Insert Produk!',
  67.                 'data' => null,
  68.             ], 200);
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement