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

AbdulBazith's avatar

How to use registration form for two tables

Guys i am working with a project job portal.

I have a register form which has fields such as(company_name, company_type,description, address, email_id, phone number, logo)

when i click submit i have a controller "company_registrationController" in that i have given the sotre method to insert the data i db

at the same time the email-id, phone number,and the name of the company must be inserted into the user table??

how it is possible??

i need to use the email_id as username and phone number as password.

Kindly some one suggest please

0 likes
10 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
public function store(Request $request) {
    User::create([
        'name' => $request->company_name,
        'email' => $request->email_id,
        'password' => Hash::make($request->phone_number),
    ]);
    
    Company::create([
        'name' => $request->company_name,
        'type' => $request->company_type,
        'description' => $request->description,
        'address' => $request->address,
        'email' => $request->email_id,
        'phone' => $request->phone_number,
        'logo' => $logoName,
    ]);
}

Like so, make sure you validate them, and don't forget about $fillable array in Company model

1 like
AbdulBazith's avatar

@Sergiu17 thank youu soo much for you response

one doubt

for what reason we are giving $fillable array in model??

i referred in google most of the answer is mass assignment is the reason.

what is mass assignment??

1 like
Sergiu17's avatar
Company::create($request->all()); // -> this is mass assignment

// now let's say your fillable array is
class Company extends Model {
    $fillable = ['name', 'email'];
}

// This means, Company model allows only name and email to insert into database.
// Why this? Because $request may contain data which you don't need.
// alternative way, for this you don't need fillable array
$company = new Company();
$company->name = $request->name;
$company->email = $reuqest->email;
$company->save();
// this is manual way

Result is the same in both cases

2 likes
mac03733's avatar

@Sergiu17 .. i was just passing by ...like the way u answer ... please keep it up

1 like
AbdulBazith's avatar

@Sergiu17 thank you sooo much, great explanation man..

actually i followed the second method in my old project as well as in my new project..

and another doubt in my registercontroller.php

protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }


the password is encrypted using 'password' => bcrypt($data['password']), this..

but now i am going to use

'password' => Hash::make($request->phone_number),

is both are same?? else which one is better

why i am asking because security is some what very important to my project that's why

1 like
Sergiu17's avatar

bcrypt is a helper function ( we call them 'wrapper' ) which calls make method from Hash facade.

So, answer is: yes, this is the same thing.

To see all helper functions: vendor/laravel/framework/src/Illuminate/Foundation/helpers.php search for bcrypt function

1 like
AbdulBazith's avatar

@Sergiu17 another small doubt

public function store(Request $request) {
    User::create([
        'name' => $request->company_name,
        'email' => $request->email_id,
        'password' => Hash::make($request->phone_number),
    ]);
  

here i need to add the company registration id also

like

'company_registration_id' =>"?" // here(in the place of question mark) what i need to give to

store the company id(its primary key) to store in the users table

1 like
Sergiu17's avatar
$company = Company::create($requeste->all());
// now $company is the newly create company, and you have access to $company->id

// Create user
User::create([
    'name' => $request->company_name,
    'email' => $request->email_id,
    'password' => Hash::make($request->phone_number),
    'company_id' => $company->id, // here's what you need
]);

As easy as that. Updated!

1 like
AbdulBazith's avatar

@Sergiu17 thank you.

If possible Kindly refer my another post and suggest your ideas please

Need suggestion for authentication table for my application is my post

1 like

Please or to participate in this conversation.