app/Http/Controllers/VariationController.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)
    {
        $inputs = $request->all();
        $validation = (new Variation)->validation($inputs);
        if($validation->fails()) {
            return jsonResponse(false, 206, $validation->getMessageBag());
        }
        try {
            \DB::beginTransaction();
            $variation = [
                'category_id' => $inputs['category_id'],
                'variation_name' => $inputs['variation_name'],
                'status' => $inputs['status'],
                'description' => $inputs['description'],
                'created_by' => authId()
            ];
            $variationId = (new Variation)->store($variation);
            if($variationId) {
                $details = [];
                if(isset($inputs['variation_title']) && count($inputs['variation_title']) > 0) {
                    foreach($inputs['variation_title'] as $i => $row) {
                        $image = null;
                        $file = $request->file('variation_image')[$i];
                        if ($file) {
                            $filename = $file->getClientOriginalName();
                            $filename = str_replace(' ', '', $filename);
                            $filename = str_random(8) . '_' . $filename;
                            $uploadPath = env('VARIATION_PATH');
                            if ($file->move($uploadPath, $filename)) {
                                $image = $filename;
                            }
                        }
                        $details[] = [
                            'variation_id' => $variationId,
                            'title' => $inputs['variation_title'][$i],
                            'price' => $inputs['variation_price'][$i],
                            'status' => $inputs['variation_status'][$i],
                            '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.created', ['attr' => __('variation.variation')]);
            return jsonResponse(true, 201, $message, $extra);
        }
        catch(\Exception $e) {
            \DB::rollBack();
            return jsonResponse(true, 207, __('message.server_error'));
        }
    }
public function store ( Request $ request ) { $ inputs = $ request -> all ( ) ; $ validation = ( new Variation ) -> validation ( $ inputs ) ; if ( $ validation -> fails ( ) ) { return jsonResponse ( false , 206 , $ validation -> getMessageBag ( ) ) ; } try { \ DB :: beginTransaction ( ) ; $ variation = [ 'category_id' => $ inputs [ 'category_id' ] , 'variation_name' => $ inputs [ 'variation_name' ] , 'status' => $ inputs [ 'status' ] , 'description' => $ inputs [ 'description' ] , 'created_by' => authId ( ) ] ; $ variationId = ( new Variation ) -> store ( $ variation ) ; if ( $ variationId ) { $ details = [ ] ; if ( isset ( $ inputs [ 'variation_title' ] ) && count ( $ inputs [ 'variation_title' ] ) > 0 ) { foreach ( $ inputs [ 'variation_title' ] as $ i => $ row ) { $ image = null ; $ file = $ request -> file ( 'variation_image' ) [ $ i ] ; if ( $ file ) { $ filename = $ file -> getClientOriginalName ( ) ; $ filename = str_replace ( ' ' , '' , $ filename ) ; $ filename = str_random ( 8 ) . '_' . $ filename ; $ uploadPath = env ( 'VARIATION_PATH' ) ; if ( $ file -> move ( $ uploadPath , $ filename ) ) { $ image = $ filename ; } } $ details [ ] = [ 'variation_id' => $ variationId , 'title' => $ inputs [ 'variation_title' ] [ $ i ] , 'price' => $ inputs [ 'variation_price' ] [ $ i ] , 'status' => $ inputs [ 'variation_status' ] [ $ i ] , '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.created' , [ 'attr' => __ ( 'variation.variation' ) ] ) ; return jsonResponse ( true , 201 , $ message , $ extra ) ; } catch ( \ Exception $ e ) { \ DB :: rollBack ( ) ; return jsonResponse ( true , 207 , __ ( 'message.server_error' ) ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
// variation code start // variation code end // variation detail code start // image code start // image code end // variation detail code end