How does the calculation?

There is a user model, order, data (order_data)
I do so:
$orders = $user->orders;
$orders->load('data');


Thus one set pulls the "data" to all the elements of array orders

But in the order I want to add the attribute "status" (it is not originally in the database), which is based on values which are in "data"

So I did the following:
// In the model Order
public function getStatusAttribute()
{
 $this->attributes['status'] = null; // showed laravel I have this attribute (without this not work)

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

// In controller
$orders = $user->orders;
$orders->load('data'); // (2)

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

(1) here in this place, namely '->data->' - in this case "data" is the value which I have received before in place of(2) or laravel makes one query for each order?
June 7th 19 at 14:49
2 answers
June 7th 19 at 14:51
Solution
Maybe like this:
return $this->attributes['status'] = $this->data->some_param;

In the model class to add not to forget
protected $appends = [ 'status' ];
No, You do not understand..
All appears as it should, but I do not understand, laravel is doing in each order, a new request for location data or takes the data which is received in paragraph (2) - Holly.Bahringer commented on June 7th 19 at 14:54
Yes, about the appends of course thanks :)
But the question mostly here in another - Holly.Bahringer commented on June 7th 19 at 14:57
Well, judging by the code you already load the data via load(). Then additional requests should not be.
Set yourself this: barryvdh/laravel-debugbar
There in socket can be clearly seen and what is happening - Gerhard commented on June 7th 19 at 15:00
June 7th 19 at 14:53
Solution
You can check
<?
DB::enableQueryLog();
// some code ... 
DB::getQueryLog();
?>


https://stackoverflow.com/questions/18236294/how-d...
Oh, well
Thank you - Holly.Bahringer commented on June 7th 19 at 14:56

Find more questions by tags Laravel