Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RayC's avatar
Level 10

Unable to create record user_id does not have a default value.

When running the below code to create a new record I get the error: "Unable to create record, user_id does not have a default value."

This would make sense, except there is a value but it is not being picked up using the following code:

KpiLog::create([
    'dealer_id' => $this->dealer_id,
    'user_id' => $this->salesperson_id,
    'greets' => $this->greets,
    'sit_downs' => $this->sit_downs,
    'write_ups' => $this->write_ups,
    'showroom_sold' => $this->showroom_sold,
    'calls_made' => $this->calls_made,
    'emails_sent' => $this->texts_sent,
    'texts_sent' => $this->calls_made,
    'appts_set' => $this->calls_made,
    'appts_shown' => $this->texts_sent,
    'appts_no_show' => $this->texts_sent,
    'log_date' => $this->log_date,
]);

I have used dd($this->salesperson_id) and it is indeed being sent.

Now if I change the above code to this, it works exactly as expected:

$kpiLog = new KpiLog;
$kpiLog->dealer_id = $this->dealer_id;
$kpiLog->user_id = $this->salesperson_id;
$kpiLog->greets = $this->greets;
$kpiLog->sit_downs = $this->sit_downs;
$kpiLog->write_ups = $this->write_ups;
$kpiLog->showroom_sold = $this->showroom_sold;
$kpiLog->calls_made = $this->calls_made;
$kpiLog->emails_sent = $this->emails_sent;
$kpiLog->texts_sent = $this->texts_sent;
$kpiLog->appts_set = $this->appts_set;
$kpiLog->appts_shown = $this->appts_shown;
$kpiLog->appts_no_show = $this->appts_no_show;
$kpiLog->log_date = $this->log_date;
$kpiLog->save();

I am running:

Laravel v9.22.1

Livewire v2.10.6

While it is working properly using the second vesion above, I am curious as to why it will not work using KpiLog::create()

If anyone can shed some light on this I would greatly appreciate it.

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Sounds like user_id isnt in the $fillable array on KpiLog

RayC's avatar
Level 10

@Sinnbeck you're right, lol it's 4:30 AM and I must be more tired than I thought I was to miss that.

Keshi's avatar

You need to add $user_id to your fillable variable in your model. Ie

protected $fillable = ['dealer_id','user_id','greets'];

Please or to participate in this conversation.