app/Http/Controllers/MaterialController.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 Material)->fetch($id);
        if(!$result) {
            $message = __('common.invalid_id', ['attr' => __('material.material')]);
            return jsonResponse(false, 207, $message);
        }
        $inputs = $request->all();
        $validation = (new Material)->validation($inputs, $id);
        if($validation->fails()) {
            return jsonResponse(false, 206, $validation->getMessageBag());
        }
        try {
            \DB::beginTransaction();
            if($request->hasFile('image')) {
                $path = env('MATERIAL_PATH');
                $image = uploadImg($request, 'image', $path);
                if($image) {
                    $inputs['image'] = $image;
                    removeImg($result->image, $path);
                }
            }
            $inputs['updated_by'] = authId();
            (new Material)->store($inputs, $id);
            \DB::commit();
            $extra = ['redirect' => route('material.index')];
            $message = __('message.updated', ['attr' => __('material.material')]);
            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 Material ) -> fetch ( $ id ) ; if ( ! $ result ) { $ message = __ ( 'common.invalid_id' , [ 'attr' => __ ( 'material.material' ) ] ) ; return jsonResponse ( false , 207 , $ message ) ; } $ inputs = $ request -> all ( ) ; $ validation = ( new Material ) -> validation ( $ inputs , $ id ) ; if ( $ validation -> fails ( ) ) { return jsonResponse ( false , 206 , $ validation -> getMessageBag ( ) ) ; } try { \ DB :: beginTransaction ( ) ; if ( $ request -> hasFile ( 'image' ) ) { $ path = env ( 'MATERIAL_PATH' ) ; $ image = uploadImg ( $ request , 'image' , $ path ) ; if ( $ image ) { $ inputs [ 'image' ] = $ image ; removeImg ( $ result -> image , $ path ) ; } } $ inputs [ 'updated_by' ] = authId ( ) ; ( new Material ) -> store ( $ inputs , $ id ) ; \ DB :: commit ( ) ; $ extra = [ 'redirect' => route ( 'material.index' ) ] ; $ message = __ ( 'message.updated' , [ 'attr' => __ ( 'material.material' ) ] ) ; 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 .
// image upload code start // image upload code end