Is the company_id mass-assignable in your User model, i.e. named in the $fillable property?
Eloquent Create or Save [ $user = User::create ] vs [ $user = new User; $user->save(); ]
I'm new to Laravel, my company has chosen to use it for re-designing our current software and I'm thoroughly enjoying it. But I've come across my first issue, hopefully I'm just being a newbie and someone can point me in the right direction.
I have a registration form which once completed and validated, the RegistrationController will complete two insertions, one into two different tables in a MySQL database.
A. Insert Company into companies table
B. Insert a User into users table.
Inside the User table there is a company_id. And the relationship of the User Model belongsTo a Company. And the Company Model hasMany Users. **Because of this relationship, I have given company_id a default value of 0 in the User table of the database.
I insert the company first so that I can retrieve the company_id so when I want to insert the user I can assign them to the relevant company.
I have done this using the Eloquent Create method as shown below. I understand this method can be shortened but I have done it the long way just to see if I was doing something wrong.
$company = Company::create([
'name' => request()->companyName,
'email' => request()->companyEmail,
'telephone' => request()->companyTelephone,
'address' => request()->companyAddress,
'town' => request()->companyTown,
'county' => request()->companyCounty,
'postcode' => request()->companyPostcode
]);
This works absolutely fine. Then I wanted to use the same method for inserting my user.
$user = User::create([
'name' => request()->adminName,
'email' => request()->adminEmail,
'password' => encrypt( request()->adminPassword),
'company_id' => $company->id
]);
This is where it goes wrong. I have dd($company->id) and got the correct value (1 in this case as it's the first entry). But when I look at the users table in the database; the company_id is gets set to 0. If I remove the default value of 0, then I get a MySQL constraint error.
So I swapped out 'company_id' = $company->id and replaced the second half of the expression with a Select query, no luck. I tried a raw query, no luck.
I finally got it working by changing from an Eloquent create to the method below.
$user = new User;
$user->name = request()->adminName;
$user->email = request()->adminEmail;
$user->password = encrypt(request()->adminPassword);
$user->company_id = $company->id;
$user->save();
Now, my question is; am I doing some thing wrong in the User::create method?
Or is it correct that the default overrides the parameter? And if so, is there a way I can override that value?
Complete code:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Company;
use Illuminate\Support\Facades\DB;
class RegistrationController extends Controller {
public function create() {
return view('forms.register');
}
public function store() {
$this->validate(request(), [
'companyName' => 'required',
'companyEmail' => 'required|email|unique:companies,email',
'companyTelephone' => 'required',
'companyAddress' => 'required',
'companyTown' => 'required',
'companyCounty' => 'required',
'companyPostcode' => 'required',
'adminName' => 'required',
'adminEmail' => 'required|email',
'adminPassword' => 'required',
'adminPasswordConfirm' => 'required',
]);
$company = Company::create([
'name' => request()->companyName,
'email' => request()->companyEmail,
'telephone' => request()->companyTelephone,
'address' => request()->companyAddress,
'town' => request()->companyTown,
'county' => request()->companyCounty,
'postcode' => request()->companyPostcode
]);
$user = new User;
$user->name = request()->adminName;
$user->email = request()->adminEmail;
$user->password = encrypt(request()->adminPassword);
$user->company_id = $company->id;
$user->save();
/**$user = User::create([
'name' => request()->adminName,
'email' => request()->adminEmail,
'password' => encrypt( request()->adminPassword),
'company_id' => $company->id
]);**/
auth()->login($user);
return redirect()->home();
}
}
Please or to participate in this conversation.