I use Laravel Auth so that users can register. What I'm trying to do now: After registering users (if they have a special role), there is another row inserted into another table (then users), which contains the associated user ID. This is the code for it:
<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; use App\Complaint; class RegisterController extends Controller { use RegistersUsers; protected $redirectTo = '/home'; public function __construct() { $this->middleware('guest'); } protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', 'username' => 'required|unique:users', 'role' => 'required' ]); } protected function create(array $data) { $user = new User; $user->name = $data['name']; $user->email = $data['email']; $user->username = $data['username']; $user->password = bcrypt($data['password']); $user->role = $data['role']; $user->templateURL = ""; $user->save(); if($data['role'] == 'Verkäufer'){ $complaintRow = Complaint::create([ 'user_id' => $user->id, 'complaintCount' => 0 ]); } switch($data['role']){ case 'Käufer': $user->attachRole(2); break; case 'Verkäufer': $user->attachRole(3); break; default: $user->attachRole(2); break; } return $user; } }
But it doesn’t work correctly, the user is inserted in the same way as the complaint line, but somehow $user->id seems to be null, the column always has user_id set to 0. Any ideas why this might look like this?
EDIT : I got it now ... Actually this is not the code I wrote, I just did not make the user_id field fillable in the complaint table, so it was always 0 because 0 was the default value, so it just didn't set it .
Thanks everyone for the answers anyway.
source share