Nikolas Flauschig Nikolas Flauschig - 1 year ago 124
PHP Question

Laravel unique registraion token

I try to make registration only for people they have a token (a randomly generated key) but I have the Problem that I cant compare my token input with the database.

I tried this:

$token = Tokens::findOrFail($data['register_token']);

if($token) {

return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'register_token' => $data['register_token'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'role' => 'student',

else {

return redirect('/register')->with('unkown_token', 'This Token does not exist!');


I always get an 404 Not found Error. I generated tokens with
$string = str_random(40);


Answer Source

FindorFail works on primary key.

For custom column, add protected $primaryKey='custom_column_name' in the model. But if you don't want to do so then try regular conditional query given below.

$token = Tokens::where('register_token',$data['register_token'])->firstOrFail();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download