Laravel集合中的访问属性

时间:2017-01-06 19:40:00

标签: laravel laravel-5

这与我之前提出的另一个问题无关,但该问题未能成功解答。 我想我已经缩小范围以使问题更加准确。 我在Laravel中有一个父/子BelongsTo关系,它返回以下内容(这是{{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 ▶}
    }

我可以看到"名称"属性在那里,我试图访问" name"的属性。以下列方式但我收到错误"试图获得非对象的属性"。

$task->asset->parent_asset->name

我应该如何访问"名称"集合中的属性?

模型关系:

这是资产模型中的关系:

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

和任务模型中的关系

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

查询$task = Task::with('asset.parent_asset')->first();

上的dd($ task)结果
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
}

2 个答案:

答案 0 :(得分:1)

经过多次震动后我找到了答案:

这不是我需要打电话的: $task->asset->parent_asset->name

是这样的: $task->asset->parent_asset['name']

我不确定为什么在这种情况下它是一个数组但是我做了dd($task->asset->parent_asset)它显示了一个数组。一旦我在视图中更改为数​​组,它就像我想要的那样工作。

答案 1 :(得分:0)

我只能假设你的人际关系正在发挥作用。

您正在转储关系的定义,而不是查询的结果。

尝试将此用于查询:

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

然后像这样转储:

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

假设您的关系定义正确,这应该可以为您提供所需的结果。

修改

事实证明,为了使用魔法属性,必须使用camelCased。更多Laravel 4 - Can't retrieve data in a one-to-many relationship

parent_asset()方法重命名为parentAsset()并尝试上述查询。