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

Anotheruser's avatar

How to populate multiple database tables form a single form

Hi.

I am building a sign up form which consists of a number of input fields imcluding.

  • Firstname
  • Lastname
  • CompanyName .........

When the form is submitted I need to create a new Company in the Companies table and a new User within the Users table.

Currently I have 3 controllers. JoinController which receives the form input, UserController and CompanyController. The User controller is also used elsewhere in the application.

My problem is that I don't want to go down the route of doing something like $user = new User(); and $company = new Company(); in order to populate the database from within the Joincontroller as this should be taken care of by the User and Company controllers themselves. I need a way to pass the appropriate fields from the JoinController into each of the UserController and CompanyController's. I know controllers aren't supposed to talk to each other so i'm stumped on how to achieve this.

Any advice very much welcome.

Thanks

0 likes
12 replies
bashy's avatar

You'll want some relationships on those so you can automatically create them both and assign them to each other.

Have you got any code done so far? Relationships?

Anotheruser's avatar

Hi bashy my models are defined as follows currently.

Company model:

<?php

class Company extends Eloquent {

public function Users() { return $this->hasMany('User'); }

}

User model:

<?php

use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

public function Company() { return $this->belongsTo('Company'); }

/** * The database table used by the model. * * @var string */ protected $table = 'users';

/** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = array('password', 'remember_token');

}

bashy's avatar

I'm not sure on this but you may need to tweak it a little

$company = Company::create(['name' => Input::get('company_name')]);

$company->user()->insert(['username' => Input::get('username')]);

Never inserted two models and related them before.

There's also some other methods like $user->push()

Anotheruser's avatar

No problem. Lets say for now that they arent related and i just need to populate two database tables with specifics fields of the same form.

Can you advise on a way to do that?

Thanks.

bashy's avatar

In that case, just copy the first line and do the same for the User model :)

$company = Company::create(['name' => Input::get('company_name')]);

$user = User::create(['username' => Input::get('username')]);

Or

$company = new Company;
$company->name = Input:get('company_name');
$company->save();

$user = new User;
$user->username = Input:get('username');
$user->save();

// If you need to access the IDs
// $user->id;
// $company->id;
Anotheruser's avatar

This was my query really. isnt using

$company = new Company; $company->name = Input:get('company_name'); $company->save();

within joinController bad practice? shouldnt that be taken care of by CompanyController and UserController?

bashy's avatar

Oh I understand the last bit in your question now.

It depends what joinController is for and what Laravel version

Anotheruser's avatar

JoinController is basically for handling the form, for displaying and capturing the data input into it. Im using laravel 4.2

arabsight's avatar

you can use repositories to keep your controller clean.

Anotheruser's avatar

I'll look into that. I don't suppose you have an example based around my issue?

arabsight's avatar

a very simple example would be something like this for the controller:

class CompanyController extends Controller {

 private $companyRepository;
 private $userRepository;

 public function __construct(ICompanyRepository $companyRepository,
        IUserRepository $userRepository)
 {
  $this->companyRepository = $companyRepository;
  $this->userRepository = $userRepository;
 }

 public function store()
 {
  $company = $this->companyRepository->add(Input::only('company_name'));
  $user = $this->userRepository->add(Input::only('username'));
 }
}

and for the repo:

class CompanyRepository implements IRepository {

    private $company;

    function __construct(Company $company)
    {
        $this->company = $company;
    }

    public function add(array $input)
    {
        return $this->company->create($input);
    }
} 

of course you need to define the Interfaces and the binding with IoC container as well.

1 like

Please or to participate in this conversation.