0
votes

Laravel 5.4, php 5.6 try to updateOrCreate password reset token her is my attempt.

$passwordReset = ( new PasswordReset )->updateOrCreate(
                ['email' => $user->email],
                [
                    'email' => $user->email,
                    'token' => str_random(60)
                ]
            );

create working fine if the user reset his password for the first time, but when update I got this QueryException

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update password_resets set token = 8ALMd5schQR9KNQ5kIt89bwr5lu5mb5G1DpYLttkizN6Z5ZxCcyhYaBp0X97 where id is null)

1
What is the primary key name in your table? - pr1nc3
@pr1nc3 my primary key name email - Yousef Altaf

1 Answers

3
votes

The problem with your code is that laravel pre-defines your primary key to be id.

You have 3 solutions:

1) Either you add an id column, autoincrement and make it your primary key

2) Simple use:

protected $primaryKey = 'email'; 

to re-define your primary key in your Model,

3) Lastly you put a where clause by yourself:

->where('email',$user->email)