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)
    {
        $validatedData = $request->validate([
            'name'  => 'required|max:255',
            'price' => '',
        ]);
        $input = $request->all();
        $arrWithPartsId = [];
        foreach($request->input('partquantity') as $item)
        {
            $arrWithPartsId['item'] = [
                'id' => $request->input('id'),
                'quantity' => $request->input('partquantity')
            ];
        }
        $product = Products::find($id);
        $product->name = $request->input('name');
        $product->price = $request->input('price');
        if ($image = $request->file('image')) {
            $destinationPath = 'image/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image'] = "$profileImage";
            $product->update(['image' => $profileImage]);
        }
        $product->parts = json_encode($arrWithPartsId);
        $product->update($input);
        return redirect('/products/'.$id.'/edit')->with('success', 'Game is successfully saved');
    }
public function update ( Request $ request , $ id ) { $ validatedData = $ request -> validate ( [ 'name' => 'required|max:255' , 'price' => '' , ] ) ; $ input = $ request -> all ( ) ; $ arrWithPartsId = [ ] ; foreach ( $ request -> input ( 'partquantity' ) as $ item ) { $ arrWithPartsId [ 'item' ] = [ 'id' => $ request -> input ( 'id' ) , 'quantity' => $ request -> input ( 'partquantity' ) ] ; } $ product = Products :: find ( $ id ) ; $ product -> name = $ request -> input ( 'name' ) ; $ product -> price = $ request -> input ( 'price' ) ; if ( $ image = $ request -> file ( 'image' ) ) { $ destinationPath = 'image/' ; $ profileImage = date ( 'YmdHis' ) . " . " . $ image -> getClientOriginalExtension ( ) ; $ image -> move ( $ destinationPath , $ profileImage ) ; $ input [ 'image' ] = " $ profileImage " ; $ product -> update ( [ 'image' => $ profileImage ] ) ; } $ product -> parts = json_encode ( $ arrWithPartsId ) ; $ product -> update ( $ input ) ; return redirect ( '/products/' . $ id . '/edit' ) -> with ( 'success' , 'Game is successfully saved' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .