app/Http/Controllers/ProductController.php
//to create ad you need 3 steps
to create ad you need 3 steps
to create ad you need 3 steps
public function save_first_step(Request $request)
    {
        $input = $request->all();
        $validator = Validator::make($input, [
            'category_id' => 'required',
            'sub_category_id' => '',
            'title' => 'required',
            'main_image' => 'required',
            'images' => '',
            'city_id' => 'required|exists:cities,id',
            'area_id' => 'required|exists:areas,id',
            'latitude' => '',
            'longitude' => '',
            'options' => ''
        ]);
        if ($validator->fails()) {
            $response = APIHelpers::createApiResponse(true, 406, $validator->messages()->first(), $validator->messages()->first(), null, $request->lang);
            return response()->json($response, 406);
        } else {
            $user = auth()->user();
            if ($user != null) {
                $input['user_id'] = $user->id;
                $mytime = Carbon::now();
                $today = Carbon::parse($mytime->toDateTimeString())->format('Y-m-d H:i');
                $input['publish'] = 'Y';
                $input['publication_date'] = $today;
                $image = $request->main_image;
                Cloudder::upload("data:image/jpeg;base64," . $image, null);
                $imagereturned = Cloudder::getResult();
                $image_id = $imagereturned['public_id'];
                $image_format = $imagereturned['format'];
                $image_new_name = $image_id . '.' . $image_format;
                $input['main_image'] = $image_new_name;
                if ($input['price'] == null) {
                    $input['price'] = '0';
                }
                $settings = Setting::find(1);
                $mytime = Carbon::now();
                $today = Carbon::parse($mytime->toDateTimeString())->format('Y-m-d H:i');
                $final_retweet_date = Carbon::createFromFormat('Y-m-d H:i', $today);
                $final_pin_date = Carbon::createFromFormat('Y-m-d H:i', $today);
                $final_expire_pin_date = $final_pin_date->addDays($settings->expier_days);
                $input['expiry_date'] = $final_expire_pin_date;
                $final_retweet_date_date = $final_retweet_date->addDays(1);
                $input['retweet_date'] = $final_retweet_date_date;
                $ad_data = Product::create($input);
                if ($request->options != null) {
                    foreach ($request->options as $key => $option) {
                        if ($option['option_value'] != null) {
                            if (is_numeric($option['option_value'])) {
                                $option_values = Category_option_value::where('id', $option['option_value'])->first();
                                if ($option_values != null) {
                                    $feature_data['type'] = 'option';
                                } else {
                                    $feature_data['type'] = 'manual';
                                }
                            } else {
                                $feature_data['type'] = 'manual';
                            }
                            $feature_data['product_id'] = $ad_data->id;
                            $feature_data['target_id'] = $option['option_value'];
                            $feature_data['option_id'] = $option['option_id'];
                            Product_feature::create($feature_data);
                        }
                    }
                }
                if ($request->images != null) {
                    foreach ($request->images as $image) {
                        Cloudder::upload("data:image/jpeg;base64," . $image, null);
                        $imagereturned = Cloudder::getResult();
                        $image_id = $imagereturned['public_id'];
                        $image_format = $imagereturned['format'];
                        $image_name = $image_id . '.' . $image_format;
                        $data['product_id'] = $ad_data->id;
                        $data['image'] = $image_name;
                        ProductImage::create($data);
                    }
                }
                $response = APIHelpers::createApiResponse(false, 200, 'your ad added successfully', 'تم أنشاء الاعلان بنجاح', null, $request->lang);
                return response()->json($response, 200);
            } else {
                $response = APIHelpers::createApiResponse(true, 406, '', 'يجب تسجيل الدخول اولا', null, $request->lang);
                return response()->json($response, 406);
            }
        }
    }
public function save_first_step ( Request $ request ) { $ input = $ request -> all ( ) ; $ validator = Validator :: make ( $ input , [ 'category_id' => 'required' , 'sub_category_id' => '' , 'title' => 'required' , 'main_image' => 'required' , 'images' => '' , 'city_id' => 'required|exists:cities,id' , 'area_id' => 'required|exists:areas,id' , 'latitude' => '' , 'longitude' => '' , 'options' => '' ] ) ; if ( $ validator -> fails ( ) ) { $ response = APIHelpers :: createApiResponse ( true , 406 , $ validator -> messages ( ) -> first ( ) , $ validator -> messages ( ) -> first ( ) , null , $ request -> lang ) ; return response ( ) -> json ( $ response , 406 ) ; } else { $ user = auth ( ) -> user ( ) ; if ( $ user != null ) { $ input [ 'user_id' ] = $ user -> id ; $ mytime = Carbon :: now ( ) ; $ today = Carbon :: parse ( $ mytime -> toDateTimeString ( ) ) -> format ( 'Y-m-d H:i' ) ; $ input [ 'publish' ] = 'Y' ; $ input [ 'publication_date' ] = $ today ; $ image = $ request -> main_image ; Cloudder :: upload ( " data:image/jpeg;base64, " . $ image , null ) ; $ imagereturned = Cloudder :: getResult ( ) ; $ image_id = $ imagereturned [ 'public_id' ] ; $ image_format = $ imagereturned [ 'format' ] ; $ image_new_name = $ image_id . '.' . $ image_format ; $ input [ 'main_image' ] = $ image_new_name ; if ( $ input [ 'price' ] == null ) { $ input [ 'price' ] = '0' ; } $ settings = Setting :: find ( 1 ) ; $ mytime = Carbon :: now ( ) ; $ today = Carbon :: parse ( $ mytime -> toDateTimeString ( ) ) -> format ( 'Y-m-d H:i' ) ; $ final_retweet_date = Carbon :: createFromFormat ( 'Y-m-d H:i' , $ today ) ; $ final_pin_date = Carbon :: createFromFormat ( 'Y-m-d H:i' , $ today ) ; $ final_expire_pin_date = $ final_pin_date -> addDays ( $ settings -> expier_days ) ; $ input [ 'expiry_date' ] = $ final_expire_pin_date ; $ final_retweet_date_date = $ final_retweet_date -> addDays ( 1 ) ; $ input [ 'retweet_date' ] = $ final_retweet_date_date ; $ ad_data = Product :: create ( $ input ) ; if ( $ request -> options != null ) { foreach ( $ request -> options as $ key => $ option ) { if ( $ option [ 'option_value' ] != null ) { if ( is_numeric ( $ option [ 'option_value' ] ) ) { $ option_values = Category_option_value :: where ( 'id' , $ option [ 'option_value' ] ) -> first ( ) ; if ( $ option_values != null ) { $ feature_data [ 'type' ] = 'option' ; } else { $ feature_data [ 'type' ] = 'manual' ; } } else { $ feature_data [ 'type' ] = 'manual' ; } $ feature_data [ 'product_id' ] = $ ad_data -> id ; $ feature_data [ 'target_id' ] = $ option [ 'option_value' ] ; $ feature_data [ 'option_id' ] = $ option [ 'option_id' ] ; Product_feature :: create ( $ feature_data ) ; } } } if ( $ request -> images != null ) { foreach ( $ request -> images as $ image ) { Cloudder :: upload ( " data:image/jpeg;base64, " . $ image , null ) ; $ imagereturned = Cloudder :: getResult ( ) ; $ image_id = $ imagereturned [ 'public_id' ] ; $ image_format = $ imagereturned [ 'format' ] ; $ image_name = $ image_id . '.' . $ image_format ; $ data [ 'product_id' ] = $ ad_data -> id ; $ data [ 'image' ] = $ image_name ; ProductImage :: create ( $ data ) ; } } $ response = APIHelpers :: createApiResponse ( false , 200 , 'your ad added successfully' , 'تم أنشاء الاعلان بنجاح', null, $request->l a g); return response ( ) -> json ( $ response , 200 ) ; } else { $ response = APIHelpers :: createApiResponse ( true , 406 , '' , 'يجب تسجيل الدخول اولا', null, $request-> l ng); return response ( ) -> json ( $ response , 406 ) ; } } }
to create ad you need 3 steps
to create ad you need 3 steps
//to get the expire_date of ad //save second step of creation ... //create final //create expier day //to make retweet date //save product feature ...