Ad

Laravel Eloquent - Merge Parent $appends

- 1 answer

Let's suppose I have two Models, Veichle and Car.

Veichle extends Model {
    $appends = ['append_1', 'append_2'];
}

and

Car extends Veichle {
    $appends = ['append_3', 'append_4'];
}

How can I use append_1 if I have Car object? In this way the Car class just override the parent $appends, I didn't find any method to merge it

Ad

Answer

You can do it in the constructor by using the append method as it will append the provided attributes to the existing ones:

Car extends Vehicle
{
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->append(['append_3', 'append_4']);
    }
}
Ad
source: stackoverflow.com
Ad