circle1
circle1

Reputation: 237

Access attribute in Laravel collection

This is related to another question I asked previously that wasn't answered successfully. I think I have narrowed it down to make the question more precise. I have a parent/child BelongsTo relation in Laravel and it is returning the following (which is the result of a {{dd($task->asset->parent_asset())}}

  BelongsTo {#657 ▼
      #foreignKey: "parent_asset_id"
      #otherKey: "id"
      #relation: "parent_asset"
      #query: Builder {#663 ▶}
      #parent: Asset {#664 ▼
        #dates: array:1 [▶]
        #fillable: array:19 [▶]
        #connection: null
        #table: null
        #primaryKey: "id"
        #perPage: 15
        +incrementing: true
        +timestamps: true
        #attributes: array:24 [▼
          "id" => 4
          "agency_id" => 1
          "name" => "Bedrock Park"
          "description" => ""
          "serial_number" => ""
          "asset_category_id" => 5
          "activity_center_id" => 1
          "original_cost" => 0.0
          "in_service_date" => "1920-01-01"
          "expected_lifespan" => 0
          "status" => 1
          "notes" => ""
          "created_at" => "2015-09-14 17:59:47"
          "updated_at" => "2016-10-31 20:01:29"
          "square_feet" => 0
          "gps_longitude" => "0.000000"
          "gps_latitude" => "0.000000"
          "parent_asset_id" => 0
          "supervisor_id" => 0
          "model_number" => 0
          "is_location" => 1
          "asset_sub_category_id" => 0
          "asset_group_id" => 2
          "address" => ""
        ]
        #original: array:24 [▶]
        #relations: []
        #hidden: []
        #visible: []
        #appends: []
        #guarded: array:1 [▶]
        #casts: []
        #touches: []
        #observables: []
        #with: []
        #morphClass: null
        +exists: true
      }
      #related: Asset {#682 ▶}
    }

I can see that the "name" attribute is there and I am trying to access that attribute of "name" in the following way but I receive an error "Trying to get property of non-object".

$task->asset->parent_asset->name

How should I be accessing the "name" attribute in the collection?

Model relations:

This is the relation in the asset model:

public function parent_asset()
   {
     return $this->belongsTo('\pmms\Asset', 'parent_asset_id');
   }

and the relation in the task model

public function asset()
   {
     return $this->belongsTo('\pmms\Asset');
   }

results of dd($task) on query $task = Task::with('asset.parent_asset')->first();

Task {#738 ▼
  #dates: array:2 [▶]
  #fillable: array:20 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:23 [▼
    "id" => 71214
    "agency_id" => 1
    "sourceable_id" => 6
    "sourceable_type" => "\pmms\Primary_task_type"
    "primary_task_type_id" => 6
    "activity_center_id" => 0
    "asset_id" => 158
    "hours" => 0.0
    "labor_code_id" => 1
    "user_id" => 7
    "assigned_to_user_id" => 7
    "do_date" => "2016-10-01"
    "date_completed" => null
    "status" => 0
    "supervisor_notes" => ""
    "staff_notes" => ""
    "created_at" => "2016-07-17 21:44:22"
    "updated_at" => "2016-07-17 21:44:22"
    "priority_level" => 3
    "crew_assignment_id" => null
    "multi_asset_task_id" => null
    "scheduled_maintenance_series_id" => 23
    "pay_rate" => 0.0
  ]
  #original: array:23 [▼
    "id" => 71214
    "agency_id" => 1
    "sourceable_id" => 6
    "sourceable_type" => "\pmms\Primary_task_type"
    "primary_task_type_id" => 6
    "activity_center_id" => 0
    "asset_id" => 158
    "hours" => 0.0
    "labor_code_id" => 1
    "user_id" => 7
    "assigned_to_user_id" => 7
    "do_date" => "2016-10-01"
    "date_completed" => null
    "status" => 0
    "supervisor_notes" => ""
    "staff_notes" => ""
    "created_at" => "2016-07-17 21:44:22"
    "updated_at" => "2016-07-17 21:44:22"
    "priority_level" => 3
    "crew_assignment_id" => null
    "multi_asset_task_id" => null
    "scheduled_maintenance_series_id" => 23
    "pay_rate" => 0.0
  ]
  #relations: array:2 [▼
    "assignee" => User {#670 ▶}
    "asset" => Asset {#666 ▼
      #dates: array:1 [▶]
      #fillable: array:19 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:24 [▶]
      #original: array:24 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
    }
  ]
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
}

Upvotes: 0

Views: 8764

Answers (2)

circle1
circle1

Reputation: 237

I found the answer after much head shaking:

It is not this that I needed to call: $task->asset->parent_asset->name

It is this: $task->asset->parent_asset['name']

I am not sure why it is an array in this case but I did dd($task->asset->parent_asset) and it showed an array. Once I changed to an array in my view, it worked like I wanted.

Upvotes: 1

DevK
DevK

Reputation: 9942

I can only assume that your relationships are working.

You are dumping the definition of relationship, not result of the query.

Try using this for query:

$task = Task::with('asset.parentAsset')->first();

And then dump like this:

dd($task->asset->parent_asset->name);

Assuming your relationships are defined correctly this should give you the result you want.

Edit:

As it turns out the relationship must be camelCased in order to use the magic property. More Laravel 4 - Can't retrieve data in a one-to-many relationship

So rename your parent_asset() method to parentAsset() and try the above queries.

Upvotes: 2

Related Questions