In a Laravel project I want to use the Laravel authentication table 'users' to have a foreign field key to point to another table as layer of abstraction. Is there a way of forcing the user registration to add a row to that abstraction layer table? Its model is simple there is just one attribute.
My RegisterController: ` protected function create(array $data) {
Rekvirent::create([
'rekvirent' => $data['initialer'],
]);
return User::create([
'initialer' => $data['initialer'],
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
` I get an error message from mysql that theres a foreign key error suggesting that the rekvirent has not been inserted when it gets to inserting the row in the users table.
My rekvirent model is as follows
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Rekvirent extends Model {
public $timestamps = false; // dumps timestamps from table
public $incrementing = false; // if no autoincrementing
protected $table = 'rekvirent'; // change default snake-case name
protected $keyType = 'string'; // if key is not integer
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'initialer',
];
/*
public function user() {
return $this->hasOne('App\User');
}
*/
}
If you are looking to do certain actions once a new user is registered, you can listen to events.
If you see Illuminate/Foundation/Auth/RegistersUsers.php
:
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
You will notice laravel is internally emitting an event Illuminate\Auth\Events\Registered
. You can listen to that event and then do actions you need like inserting into a separate table etc.
See event documentation for listening an event.