oop - Issue accessing a relation in a PHP object -


i using laravel 4 , have line:

$report = deliveryprofitreport::where('id', $id)->with('car_info', 'basic_customer')->get(); 

i access 'car_info' so:

$report->car_info->id 

but getting errors time:

undefined property: illuminate\database\eloquent\collection::$car_info 

the object looks (if dd() $report):

object(illuminate\database\eloquent\collection)#823 (1) {   ["items":protected] array(1) {     [0] object(deliveryprofitreport)#798 (20) {       ["table":protected] "delivery_profit_reports"       ["guarded":protected] array(0) {      }      ["connection":protected] null      ["primarykey":protected] "id"      ["perpage":protected] 15      ["incrementing"] true      ["timestamps"] true      ["attributes":protected] array(29) {        ["id"] "13"        ["updated_at"] "2015-07-15 10:11:39"        ["created_at"] "2015-07-15 10:11:39"      }      ["original":protected] array(29) {        ["id"] "13"        ["updated_at"] "2015-07-15 10:11:39"        ["created_at"] "2015-07-15 10:11:39"      }      ["relations":protected] array(2) {        ["car_info"] object(car)#801 (29) {          ["connection":protected] "main_site"          ["timestamps"] false          ["hidden":protected] array(7) {            [0] "basic_incl_options"          } ... , on. 

the problem here get() returns array.

change first():

$report = deliveryprofitreport::where('id', $id)->with('car_info', 'basic_customer')->first(); 

which remove top level array in dd().

otherwise, you'll have like:

$report[0]->car_info->id

or $report = $report[0]; , $report->car_info->id


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -