Home > Software engineering >  Laravel Eloquent Append Exceed 60 seconds
Laravel Eloquent Append Exceed 60 seconds

Time:12-17

I have a model like this one.

class Transaction extends Model
{
    ...

    protected $appends = ['pemilik'];

    ...

    public function reference(){
        return $this->morphTo();
    }

    public function getPemilikAttribute(){
        switch ($this->attributes['reference_type']){
            case 'saving': return $this->reference->penabung;
            case 'loan': return $this->reference->peminjam;
            default: return null;
        }
    }
}

and an eloquent query like this.

$query = Transaction::with(['petugas' => function($que){
        $que->select('id', 'nama');
    }])->whereHasMorph('reference', 'saving', function($que){
        $que->where('tipe_tabungan', 'simpanan_wajib');
    });

    if($year != "all"){
        $query = $query->whereYear('created_at', $year);
    }
    if($month != "all"){
        $query = $query->whereMonth('created_at', $month);
    }
    if($position != "all") {
        $data = $query->get()->filter(function($que) use($position){
            return $que->pemilik->jabatan == $position;
        });

    } else {
        $data = $query->get();
    }

The code work fine when I tried it in thinker, but when I tried to retrieve it using postman it failed.

Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded in file C:\laragon\www\kpri-new\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 3256

#0 {main}

Why does that happen and what is the solution? I am not using Resource to modify API responses but do it directly in the controller.

CodePudding user response:

You can set max_execution_time in your php.ini and restart server:

max_execution_time=300

Or, in your PHP code:

ini_set('max_execution_time', 300); // 5 minutes

CodePudding user response:

This could happen, because of heaviness of data process and fetching the data from DB,Specially, when the related table contains many records. In my view, if you use pagination instead of get, the server would not need more time to process the data.

  • Related