tbhaxor
tbhaxor

Reputation: 1943

Getting timeout when casting null timestamp field to Carbon

I have a nullable field completed_at in the table and I want to cast it to Illuminate\Support\Carbon iff the field is not-null, otherwise it should be null.

When I call the Task::all() function, I am getting Maximum execution time of 30 seconds exceeded error.

Model configuration:

<?php

namespace App\Models;

use App\Enums\TaskStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class Task extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'description',
    ];

    protected $casts = [
        'completed_at' => Carbon::class, // This is causing timeout error
        'status' => TaskStatus::class,
    ];
}

Migration file

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->id();
            $table->string('title', 256);
            $table->text('description')->nullable();
            $table->enum('status', ['pending', 'in_progress', 'completed'])->default('pending');
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tasks');
    }
};

Upvotes: 1

Views: 56

Answers (0)

Related Questions