A
A
Artem00712018-03-02 12:54:42
Laravel
Artem0071, 2018-03-02 12:54:42

How are the calculations done?

There are models user, order, data (order_data)
I do this:

$orders = $user->orders;
$orders->load('data');

Thus, in one go, "data" is pulled up to all elements of the orders array.
But in the order, I want to add the attribute "status" (it is not initially in the database), which is based on the values ​​\u200b\u200bthat are in "data"
So I did the following:
// В модели Order
public function getStatusAttribute()
    {
        $this->attributes['status'] = null; // показал laravel что у меня есть этот атрибут (без этого не работате)

        return $this->data->some_param; // (1)
}

// В контроллере
$orders = $user->orders;
$orders->load('data'); // (2)

foreach ($orders as $order)
     $order->status;

(1) in this place, namely '->data->' - in this case, "data" is the value that I received before in place (2) or does laravel make one more request for each order?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andreo, 2018-03-02
@Artem0071

Maybe like this:
And in the model class, do not forget to add
protected $appends = [ 'status' ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question