vendredi 11 décembre 2020

Eloquent create method returning "id" 0 after storing data

I am facing an strange issue. This is my migration:

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();
    });

This is my model:

class Calendar extends Model

{

use SoftDeletes;

protected $fillable = [
    "id",
    "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.

$calendarIds[]=(Calendar::create($data))->id;

The $data variable holds:

array:12 [
  "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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire