vendredi 6 janvier 2017

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

  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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iR3hmQ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire