Try this:
$user = new User;
$user->fistname = Input::get('firstname');
$user->...
...
$user->save();
$company = new Company;
$company->company_name = Input::get('company_name');
...
...
$user->company()->save($company);
Hi usm4n,
Unfortunately it returns this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' .
Looking at the SQL statements it runs before crashing, it still doesn't appear to be selecting the fields:
insert into `assigned_roles` (`role_id`, `user_id`) values ('2', '3')
insert into `companies` (`logo_file_name`, `logo_file_size`, `logo_content_type`, `logo_updated_at`, `updated_at`, `created_at`) values ('2012-12-07 16.31.42.png', '36952', 'image/png', '2014-03-16 13:59:41', '2014-03-16 13:59:41', '2014-03-16 13:59:41').
Sorry! I did not read this line before "company has many users and a user has one company", this line IMHO, should be interpreted as "company has many users and a user belongs to one company".In this case, define a hasMany relation in your Company model, and specify a belongsTo relation in User model:
//User model
public function company()
{
return $this->belongsTo('Company');
}
...
and in Company model:
public function users()
{
return $this->hasMany('User');
}
This will also requires you to have a company_id field in Users table. This way,you can save new users for a company like this:
company->users()->save($user);
Regarding the above error, since you have defined a hasOne relationship in Users model, Eloquent will look for the foreign key named as 'user_id' in companies table, and it looks like it is not present.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community