jeudi 29 juin 2017

Custom attribute crashing on toJSON

I am trying to determine which position the order is in to generate a order id, but this crashes laravel, nothing in the logs, just a 500 error in the browser:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Load extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $appends = ['order_no'];

    public function getOrderNoAttribute()
    {
        $count = 1;
        foreach ($this->workorder->loads as $load) {
            if ($load->id == $this->id) {
                break;
            }
            $count++;
        }
        return $this->workorder->id . "-" . $count;
    }
}

When I changed it to return just an integer it worked, so I am almost certain it is the relation access causing the issue. Is there a way to do this that is better?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire