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

subham's avatar

Getting Id 0 when storing a Model using all possible methods.

I am getting id 0 when storing a Model using ->create() , ->save() and even when using it in the created event. It returns the correct id when creating one from Tinker and it also saves in database but when I use $calender->id it returns 0.

Schema::create('calendars', function (Blueprint $table) { $table->id(); $table->string("start"); $table->string("end"); $table->foreignId("learner_id")->constrained()->cascadeOnDelete(); $table->foreignId("driving_instructor_id")->nullable()->constrained()->cascadeOnDelete(); $table->foreignId("event_type_id")->constrained()->cascadeOnDelete(); $table->foreignId("licence_class_id")->constrained()->cascadeOnDelete(); $table->text("description")->nullable(); $table->timestamps(); });

class Calendar extends Model { use SoftDeletes;

protected $fillable = [
    "start",
    "end",
    "learner_id",
    "driving_instructor_id",
    "event_type_id",
    "licence_class_id",
    "description",
];

protected $appends = ["title"];

public function eventType()
{
    return $this->belongsTo(EventType::class);
}

public function learner()
{
    return $this->belongsTo(Learner::class);
}
public function transaction()
{
    return $this->hasOne(Transaction::class);
}

public function drivingInstructor()
{
    return $this->belongsTo(DrivingInstructor::class);
}

public function licenceClass()
{
    return $this->belongsTo(LicenceClass::class);
}

public function getTitleAttribute()
{
    return $this->learner->full_name;
}

}

When use the create method, it successfully creates a new entry in the database however it is returning and id 0.

dd(Calendar::create($data)->id);

The $data variable holds:

[ "title" => "" "start" => "2020-11-30T23:00:00.000Z" "startTimezone" => "" "end" => "2020-11-30T23:00:00.000Z" "endTimezone" => "" "recurrenceRule" => "" "recurrenceException" => "" "isAllDay" => true "description" => "" "driving_instructor_id" => 3 "event_type_id" => 1 "licence_class_id" => 1 ]

I have never faced this issue before. When i do the same thing with tinker however it returns the correct id. I have tried using the $calendar->save() method as well and it is returning id as 0 as well. Any help would be appreciated.

0 likes
9 replies
MichalOravec's avatar

I don't know what is the problem, but definitely remove id from fillable.

subham's avatar

Removed id from fillable but still not solved.

subham's avatar
$data = $this->extractDataFromRequest($request);
unset($data['id']);
$ids = $request->learner_id
    ? [$request->learner_id]
    : $data["learner_id"];
    if ($request->has("driving_instructor_id"))
    $data["driving_instructor_id"] = $request->has("driving_instructor_id");
foreach ($ids as $learnerId) {
    $data["learner_id"] = $learnerId;
    dd(Calendar::create($data)->id);
}
lara3896's avatar

@subham what do you get if you do this:

$newid = Calendar::create($data);
dd($newId);
subham's avatar
[{"start":"2020-12-01T23:00:00.000Z","end":"2020-12-01T23:00:00.000Z","description":"","driving_instructor_id":3,"event_type_id":1,"licence_class_id":1,"learner_id":"1","updated_at":"2020-12-18T11:27:18.000000Z","created_at":"2020-12-18T11:27:18.000000Z","id":0}]
subham's avatar
subham
OP
Best Answer
Level 1

id:0 is placed at the end of the object. I noticed that I have installed telescope package. And with query watcher disabled, id is first and has the correct value.

I had to set TELESCOPE_QUERY_WATCHER=false so I could get on with my life, but I'd really like the query watcher to work...

4 likes
internetmate's avatar

@subham Thank you! It was Telescope in the end.

I was wondering why the id in the model object worked locally (with the auto-increment value) but had id=0 on a demo server despite exactly same PHP, Laravel, Composer, Nginx ad MySQL version.

Adding TELESCOPE_QUERY_WATCHER=false to .env on my demo server resolved the problem.

1 like
tarekmia's avatar

@subham how did you resolve this behavior? I'm facing same issue, and can't found any solution. I'd also like the query watcher to function with Telescope as well.

Please or to participate in this conversation.