Laravel Eloquent渴望从数组中加载和检索值

时间:2018-03-26 21:17:01

标签: laravel eloquent

使用Laravel的Eloquent和eager loading如何检索built_load_cost数组和built_load_revenue数组中的值?

```

  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
  ]
]

```

1 个答案:

答案 0 :(得分:0)

通过雄辩的模型访问价值时,请使用camelCase,并带小写字母:

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

如果eloquent模型转换为数组,请使用snake_case访问关系:

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

另请参阅:Laravel “with” changes variable case to snake case