Ad

Undefined Index To Empty Variables (not Required Inputs)

- 1 answer

im learning about angularjs and laravel. Basically i use angular to fetch my data in the forms and than send it to laravel to grab my variables and than create the record, the problem is with one input field (mobile), if i fill the mobile input it doesnt give me any problems, but if i leave it empty (since is not required input) it fails to create the record and give me a undefined index.

How can i make in Laravel to create anyway the record if one variable in my case is empty or not created?

php code:

public function registerUser($inputData)
    {
        $user = \DB::transaction(function () use ($inputData)
        {

            $user = User::create([
                'email'              => $inputData['user']['email'],
                'name'               => $inputData['user']['name'],
                'surname'            => $inputData['user']['surname'],
                'mobilephone'        => $inputData['user']['mobilephone'],
                'birth_date'         => Carbon::createFromFormat('d-m-Y', $inputData['user']['birth_date']),
            ]);


            $user->save();


            //Return the User
            return $user;
        });

        //Return the User instance
        return $user;
    }
Ad

Answer

From what you are describing, when you don't fill in the mobile number the front end doesn't send an object which includes "$inputData['user']['mobilephone']". Which would cause an error in the script and prevent it from running. Please post the angular code responsible for transmitting the user information.

If my assumption is correct, you either need to check that these properties all exist in the $inputData before referencing them or have the front end add a dummy value when no value is provided.

OR What ever is holding the record (MySQL or whatever) may not allow this field to be null which would again prevent the insertion. But your description suggests this isn't it.

Ad
source: stackoverflow.com
Ad