app/Http/Controllers/ProductController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required',
            'description' => 'required',
            'item-image' => 'mimes:png,jpg,jpeg',
            'price' => 'required',
            'status' => 'required',
        ]);
        $product = Product::find($id);
        $product->name = $request->get('name');
        $product->description = $request->get('description');
        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()) {
            DB::table('user_products')
                        ->where('product_id', $id)
                        ->update([
                            'price' => $request->price,
                            'status' => $request->status]);
            return redirect('/my_products')->with('success', 'Product updated successfully!');
        } else {
            Session::flash('error', 'There was an problem saving the updated user info to the database. Please Try Again!');
            return redirect()->route('/products.edit', $id);
        }
    }
public function update ( Request $ request , $ id ) { $ request -> validate ( [ 'name' => 'required' , 'description' => 'required' , 'item-image' => 'mimes:png,jpg,jpeg' , 'price' => 'required' , 'status' => 'required' , ] ) ; $ product = Product :: find ( $ id ) ; $ product -> name = $ request -> get ( 'name' ) ; $ product -> description = $ request -> get ( 'description' ) ; 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 ( ) ) { DB :: table ( 'user_products' ) -> where ( 'product_id' , $ id ) -> update ( [ 'price' => $ request -> price , 'status' => $ request -> status ] ) ; return redirect ( '/my_products' ) -> with ( 'success' , 'Product updated successfully!' ) ; } else { Session :: flash ( 'error' , 'There was an problem saving the updated user info to the database. Please Try Again!' ) ; return redirect ( ) -> route ( '/products.edit' , $ id ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
// $product->update($request->all()); // // $product = Product::find($id); // // $product->name = $request->name; // // $product->description = $request->description; // // $product->update(['name' => $request->name]); // // $product->update(['description' => $request->description]); // // $product->update(); // return redirect()->route('my_products')->with('success', 'Product updated successfully!');