0

Using Laravel's Eloquent and eager loading how would one retrieve the values in the built_load_cost array and th built_load_revenue array?

```

  array:11 [▼
  "id" => 5
  "created_at" => "2018-03-24 00:19:52"
  "updated_at" => "2018-03-24 00:20:14"
  "load_number" => 1
  "dispatcher_id" => 3
  "carrier_id" => 826097731
  "customer_id" => 1
  "ip" => "127.0.0.1"
  "deleted_at" => null
  "built_load_cost" => array:6 [▼
    "id" => 4
    "created_at" => "2018-03-26 19:59:34"
    "updated_at" => "2018-03-26 19:59:34"
    "line_haul" => "100.00"
    "extra_pickups" => "0.00"
    "built_load_id" => 5
  ]
  "built_load_revenue" => array:6 [▼
    "id" => 4
    "created_at" => "2018-03-26 19:59:43"
    "updated_at" => "2018-03-26 19:59:43"
    "line_haul" => "90.00"
    "extra_pickups" => "0.00"
    "built_load_id" => 5
  ]
]

```

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
DukesNuz
  • 87
  • 1
  • 9
  • 2
    Hmm, the same way as with any other array. `$arr[index][prop]` in your case. Maybe I don't understand the question. – DevK Mar 26 '18 at 21:18
  • Yes, that is what I thought. Except it returns null. I have this query: $load = BuiltLoad::with('builtLoadCost')->with('builtLoadRevenue')->where('dispatcher_id', '=', $dispatcher_id)->where('id', '=', $id)->first(); then I use this : $line_haul = $load['built_load_revenue']['line_haul']; ... which returns null. Cannot figure out why I am getting null.I should be getting 90 – DukesNuz Mar 26 '18 at 21:39
  • How are you retrieving the data that you're getting null? – jedrzej.kurylo Mar 26 '18 at 21:42
  • @jedrzej.kurylo I able to to retrieve the values for load-number and customer_id and all values for the main array, just not from the 2 arrays mentioned in my initial question – DukesNuz Mar 26 '18 at 22:09
  • Post the code that you use to retrieve that data that gives you null – jedrzej.kurylo Mar 26 '18 at 22:19
  • @jedrzej.kurylo $load = BuiltLoad::with('builtLoadCost')->with('builtLoadRevenue')->where('dispatcher_id', '=', $dispatcher_id)->where('id', '=', $id)->first(); – DukesNuz Mar 26 '18 at 22:20
  • Not the code that loads data.but the code that you later use to get build_load_cost from the array. And how do you convert the result in $load into array? – jedrzej.kurylo Mar 26 '18 at 22:23
  • Ahh so sorry, Should also be in my comment above. Here it is.... $line_haul_revenue = $load['built_load_revenue']['line_haul']; and $line_haul_cost = $load['built_load_cost']['line_haul']; Thank you much, – DukesNuz Mar 26 '18 at 22:28

1 Answers1

0

When accessing value through eloquent model, use camelCase, with first lower case letter:

$load->builtLoadCost->line_haul
$load['builtLoadCost']['line_haul']

If the eloquent model is converted to array, use snake_case to access relations:

$load->toArray()['built_load_revenue']['line_haul']

See also: Laravel “with” changes variable case to snake case

Ben
  • 5,069
  • 4
  • 18
  • 26
  • Thank you. This $load->toArray()['built_load_revenue']['line_haul'] worked. Still not sure why my original code does not and your code works. i thought eloquent model I used was returning an array. – DukesNuz Mar 26 '18 at 23:50
  • eloquent model is not an array, but you can use it as an array because of overloading, `->toArray()` is used to serialize an eloquent model to an array – Ben Mar 27 '18 at 00:06