0

I have 2 tables:

Services 
id | name | ...

Prices 
id | service_id | day | time | price

One service can have many prices (by day in week and time in day).

When I try to get my object and turn it into array this is how it looks now:

$service = Service::with('prices')->find(1);

array
  'id' => string '1'
  'name' => string 'Test'
  'prices' => 
    array
      0 => 
        array (size=7)
          'id' => string '1'
          'service_id' => string '1'
          'day' => string '1'
          'time' => string '0'
          'price' => string '50.00'
      1 => 
        array (size=7)
          'id' => string '2'
          'service_id' => string '1'
          'day' => string '1'
          'time' => string '1'
          'price' => string '50.00'
      ...

I would like to get this service object looking like this:

array
  'id' => string '1'
  'name' => string 'Test'
  'prices' => 
    array
      1 => array
          '0' => '100.00'
          '1' => '100.00'
          ...
          '23' => '450.00'
      2 => array
          '0' => '100.00'
          '1' => '100.00'
          ...
          '23' => '450.00'

In another words, I would like that my relation returns first array that contains days (1-7), and each day is array with time (0-23) in that day.

What is the best practice to do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Filip Zelic
  • 110
  • 10

1 Answers1

1

I would have a separate helper class that does the data modification. Assuming your prices model is called Price:

class ModelFormatter {

private $pc;

public function __construct(Collection $pricesCollection){
    $this->pc = $pricesCollection;
}

public function toArray(){
    $result = [];

    foreach($this->pc as $item){
         //do checks and build up the $result array
    }

    return $result;
}

}

and then

$service = Service::with('prices')->find(1);
$prices = $service->prices;

(new ModelFormatter($prices))->toArray();

Yasen Slavov
  • 787
  • 4
  • 16
  • Wouldn't be better to leverage that task to database? To use something like query scope.. With that approach my controller would be cleaner and all the work will be on the db.. – Filip Zelic Sep 24 '14 at 13:04
  • Query scopes are used to specify ...well, the scope of the query. What you're trying to do is get a relationship with an alternative format. So one other option would be to create a separate relationship 'pricesToArray' and somehow modify the relationship so that it returns the data in this format. An example of a modified relationship can be seen here: http://stackoverflow.com/questions/24343738/getting-just-the-latest-value-on-a-joined-table-with-eloquent#24350807 – Yasen Slavov Sep 24 '14 at 13:13
  • Is there a way to store this new format back to original object $service. Something lik $service->prices = (new ModelFormatter($service->prices))->toArray(); When I tried to pass that object to javascript or var_dump I see that on prices attribute is original relation from database – Filip Zelic Sep 25 '14 at 12:30