9

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

dios231
  • 714
  • 1
  • 9
  • 21

4 Answers4

10

This answer to this question taken from here :

if($request->filled('user_id'))
Ahmad Yousef
  • 621
  • 9
  • 20
5

do this

$userInformation = new UserInformation;

if(request->has('firstname')){
   $userInformation->firstname = $request->firstname;
}
if(request->has('lastnme')){
   $userInformation->lastname = $request->lastname;
}

 // do it for all

 $User->information()->save($userInformation);

Edit: Or use Form requests, it's a better approach

Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39
  • 1
    I have just tested and `request->has('firstname')` will return true even if `firstname` is null. Maybe it is laravel version thing, im using 5.8 – Dmitry Malys Jun 03 '19 at 08:57
  • i'm not sure too but you can create a middleware that unsets null values in requests or you can just add `request->get('firstname') != null` , whatever suits you – Achraf Khouadja Jun 03 '19 at 11:07
0

Check each value and push it first into array. Then assign array.

<?php
$userArray=array();
if($request->input('firstname') != "") $userArray['firstname']=$request->input('firstname');

if($request->input('lastname') != "") $userArray['lastname']=$request->input('lastname');
if($request->input('email') != "") $userArray['email']=$request->input('email');
if($request->input('mobile_phone') != "") $userArray['mobile_phone']=$request->input('mobile_phone');
if($request->input('address') != "") $userArray['address']=$request->input('address');
if($request->input('profile_photo') != "") $userArray['profile_photo']=$request->input('profile_photo');

$userInformation = new UserInformation($userArray);
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • 3
    better use $request->has('input') , it works on empty strings and null values (in laravel 5.4 empty strings are converted to null in request so this won't work) – Achraf Khouadja Feb 12 '17 at 12:40
-1
$request->validate([
  'name'=>['required'],
])

And

if(!is_null($request->name)){}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • 1
    Welcome to Stack Overflow! Your answer contains code without explanation. It's better to include details about *how* your answer answers the question. – Kevin M. Mansour Jun 09 '23 at 22:47