Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if request input is not null before set the value

I have an API that set user settings. Because neither of inputs are mandatory I want to check first if the value exists and then set it to the model attributes in order to avoid null values.

$this->InputValidator->validate($request, [
                'firsname' => 'string',
                'lastname' => 'string',
                'email' => 'email',
                'mobile_phone' => 'string',
                'address' => 'string',
                'language' => 'string',
                'timezone' => 'string',
                'nationality' => 'string',
                'profile_photo' => 'url'
            ]);

            $userInformation = new UserInformation([
                'firstname' => $request->input('firstname'),
                'lastname' => $request->input('lastname'),
                'email' => $request->input('email'),
                'mobile_phone' => $request->input('mobile_phone'),
                'address' => $request->input('address'),
                'profile_photo' => $request->input('profile_photo')
            ]);
            $User->information()->save($userInformation);

Specificaly when one of inputs is not existin I dont want to pass it to the model. Also I dont want to make inputs required

like image 277
dios231 Avatar asked Feb 12 '17 12:02

dios231


1 Answers

This answer to this question taken from here :

if($request->filled('user_id'))
like image 140
Ahmad Yousef Avatar answered Sep 18 '22 11:09

Ahmad Yousef