I don't know what is the problem, but definitely remove id from fillable.
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.
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...
Please or to participate in this conversation.