app/Http/Controllers/DiscountsController.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)
    {
        if(auth()->user()->isAdmin()){
            $this->validate($request,[
                'dis_user'=>'required'
            ]);
            $userID = $request->input('dis_user');
            $user = User::find($userID);
            if($user === null || !($user->isClient() || $user->isNewClient())) abort(404);
            $user_discounts = $user->getAllDiscounts();
            $inputs = $request->all();
            foreach($inputs as $key=>$value){
                $subcategory = Subcategory::find($key);
                if($subcategory !== null){
                    if(!(is_numeric($value) || $value=='')) abort(404); 
                    if($user_discounts->where('subcategory_id', $key)->first() !== null){
                        $discount = $user_discounts->where('subcategory_id', $key)->first();
                        if($value > 0){
                            $discount->discount = $value;
                            $discount->save();
                        }
                        else{
                            $discount->delete();
                        }
                    }
                    else{
                        if($value > 0){
                            $discount = new Discount;
                            $discount->user_id = $user->id;
                            $discount->subcategory_id = $key;
                            $discount->discount = $value;
                            $discount->save();
                        }
                    }
                }
            }
            return redirect('/users');
        }
        else abort(404);
    }
public function store ( Request $ request ) { if ( auth ( ) -> user ( ) -> isAdmin ( ) ) { $ this -> validate ( $ request , [ 'dis_user' => 'required' ] ) ; $ userID = $ request -> input ( 'dis_user' ) ; $ user = User :: find ( $ userID ) ; if ( $ user === null || ! ( $ user -> isClient ( ) || $ user -> isNewClient ( ) ) ) abort ( 404 ) ; $ user_discounts = $ user -> getAllDiscounts ( ) ; $ inputs = $ request -> all ( ) ; foreach ( $ inputs as $ key => $ value ) { $ subcategory = Subcategory :: find ( $ key ) ; if ( $ subcategory !== null ) { if ( ! ( is_numeric ( $ value ) || $ value == '' ) ) abort ( 404 ) ; if ( $ user_discounts -> where ( 'subcategory_id' , $ key ) -> first ( ) !== null ) { $ discount = $ user_discounts -> where ( 'subcategory_id' , $ key ) -> first ( ) ; if ( $ value > 0 ) { $ discount -> discount = $ value ; $ discount -> save ( ) ; } else { $ discount -> delete ( ) ; } } else { if ( $ value > 0 ) { $ discount = new Discount ; $ discount -> user_id = $ user -> id ; $ discount -> subcategory_id = $ key ; $ discount -> discount = $ value ; $ discount -> save ( ) ; } } } } return redirect ( '/users' ) ; } else abort ( 404 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// Get and validate user // Get client's discounts // Validate inputs // If value is not valid // If discount is already set // If discount is new