A
A
Andreo2015-08-28 16:16:58
Laravel
Andreo, 2015-08-28 16:16:58

How to add an additional attribute to the model?

As an example, I give an elementary situation: it is necessary in the simplest form to calculate the difference between two fields of the table and display the result in a separate manual / virtual / non-existent attribute.
Something like this:

public function getSourceIncomingAttribute($item) {
    $this->attributes['total_profit'] = $this->attributes['source_incoming'] - $this->attributes['source_selfcost'];
}

But how should it really be or is there even a normal solution without crutches?! :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Plisko, 2015-08-28
@chupacabramiamor

RTFM
protected $appends = array('total_profit');

A
Alexey Ukolov, 2015-08-28
@alexey-m-ukolov

laravel.com/docs/5.1/eloquent-mutators

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question