app/Http/Controllers/VariationController.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)
    {
        $result = (new Variation)->fetch($id);
        if(!$result) {
            $message = __('common.invalid_id', ['attr' => __('variation.variation')]);
            return jsonResponse(false, 207, $message);
        }
        $inputs = $request->all();
        $validation = (new Variation)->validation($inputs, $id);
        if($validation->fails()) {
            return jsonResponse(false, 206, $validation->getMessageBag());
        }
        try {
            \DB::beginTransaction();
            $authId = authId();
            $inputs['updated_by'] = $authId;
            (new Variation)->store($inputs, $id);
            $details = [];
            if(isset($inputs['variation_title']) && count($inputs['variation_title']) > 0) {
                $uploadPath = env('VARIATION_PATH');
                foreach($inputs['variation_title'] as $i => $row) 
                {
                    $title = $inputs['variation_title'][$i];
                    $price = $inputs['variation_price'][$i];
                    $status = $inputs['variation_status'][$i];
                    $detailId = $inputs['variation_id'][$i];
                    $image = $inputs['variation_prev_img'][$i];
                    $file = $request->file('variation_image')[$i] ?? null;
                    if ($file) {
                        $filename = $file->getClientOriginalName();
                        $filename = str_replace(' ', '', $filename);
                        $filename = str_random(8) . '_' . $filename;
                        if ($file->move($uploadPath, $filename)) {
                            $prevImg = public_path($uploadPath . $image);
                            if($image != '' && file_exists($prevImg)) {
                                unlink($prevImg);
                            }
                            $image = $filename;
                        }
                    }
                    if(isset($detailId) && $detailId != '') {
                        $update = [
                            'title' => $title,
                            'price' => $price,
                            'status' => $status,
                            'image' => $image,
                            'updated_by' => $authId,
                        ];
                        (new VariationDetail)->store($update, $detailId);
                    }
                    else {
                        $details[] = [
                            'variation_id' => $id,
                            'title' => $title,
                            'price' => $price,
                            'status' => $status,
                            'image' => $image,
                            'created_by' => $authId,
                        ];
                    }
                }
                if(isset($details) && count($details) > 0) {    
                    (new VariationDetail)->store($details, null, true);
                }
            }
            \DB::commit();
            $extra = ['redirect' => route('variation.index')];
            $message = __('message.updated', ['attr' => __('variation.variation')]);
            return jsonResponse(true, 201, $message, $extra);
        }
        catch (\Exception $e) {
            \DB::rollBack();
            return jsonResponse(true, 207, __('message.server_error'));
        }
    }
public function update ( Request $ request , $ id ) { $ result = ( new Variation ) -> fetch ( $ id ) ; if ( ! $ result ) { $ message = __ ( 'common.invalid_id' , [ 'attr' => __ ( 'variation.variation' ) ] ) ; return jsonResponse ( false , 207 , $ message ) ; } $ inputs = $ request -> all ( ) ; $ validation = ( new Variation ) -> validation ( $ inputs , $ id ) ; if ( $ validation -> fails ( ) ) { return jsonResponse ( false , 206 , $ validation -> getMessageBag ( ) ) ; } try { \ DB :: beginTransaction ( ) ; $ authId = authId ( ) ; $ inputs [ 'updated_by' ] = $ authId ; ( new Variation ) -> store ( $ inputs , $ id ) ; $ details = [ ] ; if ( isset ( $ inputs [ 'variation_title' ] ) && count ( $ inputs [ 'variation_title' ] ) > 0 ) { $ uploadPath = env ( 'VARIATION_PATH' ) ; foreach ( $ inputs [ 'variation_title' ] as $ i => $ row ) { $ title = $ inputs [ 'variation_title' ] [ $ i ] ; $ price = $ inputs [ 'variation_price' ] [ $ i ] ; $ status = $ inputs [ 'variation_status' ] [ $ i ] ; $ detailId = $ inputs [ 'variation_id' ] [ $ i ] ; $ image = $ inputs [ 'variation_prev_img' ] [ $ i ] ; $ file = $ request -> file ( 'variation_image' ) [ $ i ] ?? null ; if ( $ file ) { $ filename = $ file -> getClientOriginalName ( ) ; $ filename = str_replace ( ' ' , '' , $ filename ) ; $ filename = str_random ( 8 ) . '_' . $ filename ; if ( $ file -> move ( $ uploadPath , $ filename ) ) { $ prevImg = public_path ( $ uploadPath . $ image ) ; if ( $ image != '' && file_exists ( $ prevImg ) ) { unlink ( $ prevImg ) ; } $ image = $ filename ; } } if ( isset ( $ detailId ) && $ detailId != '' ) { $ update = [ 'title' => $ title , 'price' => $ price , 'status' => $ status , 'image' => $ image , 'updated_by' => $ authId , ] ; ( new VariationDetail ) -> store ( $ update , $ detailId ) ; } else { $ details [ ] = [ 'variation_id' => $ id , 'title' => $ title , 'price' => $ price , 'status' => $ status , 'image' => $ image , 'created_by' => $ authId , ] ; } } if ( isset ( $ details ) && count ( $ details ) > 0 ) { ( new VariationDetail ) -> store ( $ details , null , true ) ; } } \ DB :: commit ( ) ; $ extra = [ 'redirect' => route ( 'variation.index' ) ] ; $ message = __ ( 'message.updated' , [ 'attr' => __ ( 'variation.variation' ) ] ) ; return jsonResponse ( true , 201 , $ message , $ extra ) ; } catch ( \ Exception $ e ) { \ DB :: rollBack ( ) ; return jsonResponse ( true , 207 , __ ( 'message.server_error' ) ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
// variation code start // variation code end  // detail code start // image code start // image code end // update code start // create code start // detail code end