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)
    {
        $request->validate([
            'name' => 'required',
            'description' => 'required',
            'category' => 'required',
            'item-image' => 'mimes:png,jpg,jpeg',
            'price' => 'required',
            'status' => 'required',
        ]);
        $product = new Product;
        $product->name = $request->name;
        $product->description = $request->description;
        $product->status = 1;
        $product->category = $request->category;
        if($request->hasFile('item-image')) {
            $itemImage = $request->file('item-image');
            $filename = time(). '.' .$itemImage->getClientOriginalName();
            Image::make($itemImage)->resize(300, 300)->save(public_path('/uploads/products/' . $filename));
            $product->image_path = $filename;        
        }
        if($product->save()) {
            $product_id = DB::table('products')
                            ->select('product_id')
                            ->where('created_at', NOW())
                            ->value('product_id');
            $userProduct = new UserProduct;
            $userProduct->user_id = Auth::id();
            $userProduct->product_id = $product_id;
            $userProduct->price = $request->price;
            $userProduct->status = $request->status;
            $userProduct->save();
            return redirect()->route('my_products')->with('success', 'Product created successfully!');   
        } else {
            return redirect()->route('my_products')->with('error', 'There was an error uploading the product. Please Try Again!');
        }
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'name' => 'required' , 'description' => 'required' , 'category' => 'required' , 'item-image' => 'mimes:png,jpg,jpeg' , 'price' => 'required' , 'status' => 'required' , ] ) ; $ product = new Product ; $ product -> name = $ request -> name ; $ product -> description = $ request -> description ; $ product -> status = 1 ; $ product -> category = $ request -> category ; if ( $ request -> hasFile ( 'item-image' ) ) { $ itemImage = $ request -> file ( 'item-image' ) ; $ filename = time ( ) . '.' . $ itemImage -> getClientOriginalName ( ) ; Image :: make ( $ itemImage ) -> resize ( 300 , 300 ) -> save ( public_path ( '/uploads/products/' . $ filename ) ) ; $ product -> image_path = $ filename ; } if ( $ product -> save ( ) ) { $ product_id = DB :: table ( 'products' ) -> select ( 'product_id' ) -> where ( 'created_at' , NOW ( ) ) -> value ( 'product_id' ) ; $ userProduct = new UserProduct ; $ userProduct -> user_id = Auth :: id ( ) ; $ userProduct -> product_id = $ product_id ; $ userProduct -> price = $ request -> price ; $ userProduct -> status = $ request -> status ; $ userProduct -> save ( ) ; return redirect ( ) -> route ( 'my_products' ) -> with ( 'success' , 'Product created successfully!' ) ; } else { return redirect ( ) -> route ( 'my_products' ) -> with ( 'error' , 'There was an error uploading the product. Please Try Again!' ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .