How to specify a nullable value on create function in modal in Laravel?

Vishnu Raj

In a model, how are we able to insert a nullable validated field with the create function? For example, in the RegisterController: the basic validator + the create function. If I didn't send the google_id or facebook_id, the create function returns an undefined index (obviously) on the fields. What's the proper way to insert them?

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['nullable', 'email', 'unique:users'],
        'google_id' => ['nullable', 'string', 'unique:users'],
        'facebook_id' => ['nullable', 'string', 'unique:users'],
        'twitter_id' => ['nullable', 'string', 'unique:users'],
        'phone' => ['required', 'string', 'max:10', 'min:10', 'unique:users', "regex:/(^[1-9][0-9]*$)/"],
        'country_code' => ['required', 'string', 'max:255', 'exists:'.config('countries.table_name').',calling_code'],
        'password' => ['required', 'string', 'min:8'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'phone' => $data['country_code'].$data['phone'],
        'google_id' => $data['google_id'] ? $data['google_id'] : null,
        'facebook_id' => $data['facebook_id'] ? $data['facebook_id'] : null,
        'twitter_id' => $data['twitter_id'] ? $data['twitter_id'] : null,
        'password' => Hash::make($data['password']),
    ]);
}

I know it's a fundamental question, and I do know how to ignore them, but I need to know what the correct format is.

N69S

Use the new format available in php 7

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'phone' => $data['country_code'].$data['phone'],
            'google_id' => $data['google_id']??null,
            'facebook_id' => $data['facebook_id']??null,
            'twitter_id' => $data['twitter_id']??null,
            'password' => Hash::make($data['password']),
        ]);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to specify proptypes to allow nullable, required prop value?

how to set laravel nullable function parameter

How to create a function to specify bin values in Python

how to create a jquery counter function for modal switching

How to create a click-function in a modal

How to create a modal that opens when a function is called?

Laravel: Create morphs() relationship nullable

how to pass nullable field value as parameter to a function that takes none nullable parameters?

Specify function as value in Jooq

How to specify the return value of a function based on the value of a parameter

How to specify "nullable" return type with type hints

How to specify the rotation value of an object with Unity's Update function

How to specify return value of mocked function with pytest-mock?

How to create a date column not nullable?

How to use nullable in foregin in Laravel?

How to add additional input with value in modal before form submit in laravel

How to get a nullable DateTime value?

specify default value of std::function

In Laravel migrations what is the parameter in ->nullable($value = true) for?

How to specify API objects in Bootstrap 5 modal?

how to validate a modal with laravel?

Laravel Pass Old or value to modal

Can you create a function that can use the result of a parameter to specify the value to change?

How do I specify a constraint that says "collection of nullable types"?

How to create a function returning a borrowed value?

How to create an object where value is a function?

how to create object inside function of value in javascript

How do I create a HashMap with value of function?

How to create new property and value for a function?