Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kvnkrft's avatar

Adding a "company" to the Laravel auth

I created a new application, and added authentication using the php artisan make:auth command. I then changed the user registration form only collecting 1 value, the name, to collect the first name and last name.

Now I am trying to figure out how to create a Company when a new user is registered. I created a migration, model, and controller for Company using the php artisan make:model Company -mc command.

I tested things using artisan tinker and was able to create a new company. The problem is that the user enters the company name in the form, but I am not storing the company name in the 'users' table, I have a company_id field in the users table.

So should I create the user with a placeholder company ID, then take the company name to create a new company, get the ID for that company, and update the user record with the correct company_id value?

That feels clumsy.

More than 1 user will/can have the same company_id value. I want to allow that first user to invite employees and have those users assigned the same company_id value, but that's down the road :)

0 likes
5 replies
kvnkrft's avatar

I suppose I should just be able to instantiate a new object from the Company class and user the 'company name' to create a new company before the user has been created? Oh man.

What I need is a "tutor". Haha :)

kvnkrft's avatar

This is the code in my RegisterController right now

protected function create(array $data)
    {
        $company = new \App\Company;
        $company->company_name = $data['company_name'];
        $company->save();
        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'company_id' => $company->id,
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

But I still get an error

"SQLSTATE[HY000]: General error: 1364 Field 'company_id' doesn't have a default value (SQL: insert into `users` (`first_name`, `last_name`, `email`, `password`, `updated_at`, `created_at`) values (John, Doe, [email protected], y$OmNbijLWbOVWdn1i16eHOeRUehzyc6kP1XYb5FSx.6P7WLnVyExXO, 2017-12-09 00:51:12, 2017-12-09 00:51:12))

So... I feel like I'm making progress.

kvnkrft's avatar

I got it to work by updating the User model and adding the company_id to th $fillable array.

    protected $fillable = [
        'first_name','last_name','company_id', 'email', 'password',
    ];

But my code still feels nasty and wrong.

Snapey's avatar

so, first problem. how do you handle company already existing.

Next, it might be easier to register the user then take them to a profile page where they fill in their other details including company

If you are uncomfortable using the company_id directly then use a relationship. A User should belongTo a company so after the user is created, you can;

$user->company()->create($request->only('name','address1'...etc));

Laravel will take care of inserting the company_id into the user model.

You will need to change your user migration though to allow company to be nullable because for a moment user will have no company.

tumelo-mapheto's avatar

Here is how you would handle checks for already existing companies.

\App\Company::firstOrCreate(['company_name' => $data['company_name']]);

Please or to participate in this conversation.