I'm trying to save a Model to my database. But for some reason it does not work. This is the error I get:
SQLSTATE[HY000]: General error: 1364 Field 'token' doesn't have a default value (SQL: insert into `user_registration_tokens` (`updated_at`, `created_at`) values (2022-03-21 17:40:13, 2022-03-21 17:40:13))
But when I dump my model I can see that the values have been set. But I can't find token and user_id in the sql query inside the error message.
My model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string $token;
*
* @property int $user_id;
*/
class UserRegistrationToken extends BaseModel
{
use HasFactory;
public string $token;
public int $user_id;
}
How I'm trying to save the model:
$registrationToken = new UserRegistrationToken;
$registrationToken->user_id = $userId;
$registrationToken->token = '$token';
$registrationToken->save();
NOTE: When trying to save the item trough mass assignment it works. For security reasons I don't want this.