app/Http/Controllers/ProductController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        if(Product::where('product_name',$request->nama_produk)->exists()){
            return redirect('/product')->with('gagal','Produk sudah terdaftar!');
        }
        $products_data = new Product;
        $products_data->product_name = $request->nama_produk;
        $products_data->price = $request->harga_produk;
        $products_data->description = $request->deskripsi_produk;
        $products_data->stock = $request->stok_produk;
        $products_data->weight = $request->berat_produk;
        $products_data->created_at = Carbon::now()->format('Y-m-d H:i:s');
        $products_data->updated_at = Carbon::now()->format('Y-m-d H:i:s');
        $products_data->save();
        if($request->hasFile('gambar_produk')){
            foreach ($request->file('gambar_produk') as $gambar){
                $productImage = new ProductImages;
                $productImage->product_id = $products_data->id; 
                $name= $gambar->getClientOriginalName();
                if (ProductImages::where('image_name',$name)->exists()){
                    $name = uniqid().'-'.$name;
                }
                $productImage->image_name = $name;
                $productImage->created_at = Carbon::now()->format('Y-m-d H:i:s');
                $productImage->updated_at = Carbon::now()->format('Y-m-d H:i:s'); 
                $gambar->move('image',$name);
                $productImage->save();
            }
        }
        foreach ($request->kategori as $row){
            $productCategoryDetails = new ProductCategoryDetails;
            $productCategoryDetails->product_id = $products_data->id;
            $productCategoryDetails->category_id = $row;
            $productCategoryDetails->created_at = Carbon::now()->format('Y-m-d H:i:s');
            $productCategoryDetails->updated_at = Carbon::now()->format('Y-m-d H:i:s');
            $productCategoryDetails->save();
        }
        return redirect('/product')->with('berhasil','Data produk berhasil ditambahkan!');
    }
public function store ( Request $ request ) { if ( Product :: where ( 'product_name' , $ request -> nama_produk ) -> exists ( ) ) { return redirect ( '/product' ) -> with ( 'gagal' , 'Produk sudah terdaftar!' ) ; } $ products_data = new Product ; $ products_data -> product_name = $ request -> nama_produk ; $ products_data -> price = $ request -> harga_produk ; $ products_data -> description = $ request -> deskripsi_produk ; $ products_data -> stock = $ request -> stok_produk ; $ products_data -> weight = $ request -> berat_produk ; $ products_data -> created_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ products_data -> updated_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ products_data -> save ( ) ; if ( $ request -> hasFile ( 'gambar_produk' ) ) { foreach ( $ request -> file ( 'gambar_produk' ) as $ gambar ) { $ productImage = new ProductImages ; $ productImage -> product_id = $ products_data -> id ; $ name = $ gambar -> getClientOriginalName ( ) ; if ( ProductImages :: where ( 'image_name' , $ name ) -> exists ( ) ) { $ name = uniqid ( ) . '-' . $ name ; } $ productImage -> image_name = $ name ; $ productImage -> created_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ productImage -> updated_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ gambar -> move ( 'image' , $ name ) ; $ productImage -> save ( ) ; } } foreach ( $ request -> kategori as $ row ) { $ productCategoryDetails = new ProductCategoryDetails ; $ productCategoryDetails -> product_id = $ products_data -> id ; $ productCategoryDetails -> category_id = $ row ; $ productCategoryDetails -> created_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ productCategoryDetails -> updated_at = Carbon :: now ( ) -> format ( 'Y-m-d H:i:s' ) ; $ productCategoryDetails -> save ( ) ; } return redirect ( '/product' ) -> with ( 'berhasil' , 'Data produk berhasil ditambahkan!' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//untuk menyimpan data dari form //menyimpan data di tabel produk //menyimpan nama file gambar dan menaruhnya di public //Menyimpan id dan kategori produk pada detail produk