我正在尝试在一个表中使用父/子关系。每个资产都属于父资产,这是资产表/资产模型中的另一个资产。
任务表中的每个任务都有一个asset_id记录,而由该ID标识的资产在其记录中具有parent_asset_id。我试图访问任务中使用的资产的父级的名称字段,我收到一个错误。以下是涉及的代码:
这是资产模型中的关系:
public function parent_asset()
{
return $this->belongsTo('\pmms\Asset', 'parent_asset_id');
}
和任务模型中的关系
public function asset()
{
return $this->belongsTo('\pmms\Asset');
}
Ina刀片文件我试图访问资产表中的名称字段,如下所示:
$task->asset->parent_asset->name
但是我收到了错误:
Trying to get property of non-object (View: C:\pmms\resources\views\partials\tasklist.blade.php)
我在哪里错了?
{{dd($ 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 ▶}
}
刀片模板代码:
<div class="whitebox">
@forelse ($tasks as $task)
@if($last_multi_asset_id == 0 or $last_multi_asset_id != $task->multi_asset_task_id)
<?php
if ($task->sourceable_type == '\pmms\Work_order') {
$notes = $task->sourceable->explanation;
} else {
$notes = $task->supervisor_notes;
}
?>
<div class="outstanding-task-accordion-div @if ($task->status == 1)status-in-process @elseif ($task->status == 2)status-competed @endif">
<a href="#" data-toggle="modal" data-target="#taskModal{!! $i !!}">
<div class="panel-title-accordion">
@if($tasklist_type == 'o' && $tasklist_personal != 1) <div class="col-md-2 task-header-staff">{!! $task->assignee->userName() !!}
@if(!is_null($task->crew_assignment_id)) <span class="crew text-nowrap">(Crew {!! $task->crew_assignment->id !!})</span> @endif</div> @endif
<div class="col-md-1 col-sm-2 task-header-date text-nowrap"> {!! $task->do_date->format('n/j/Y') !!}</div>
<div class="col-md-2 col-sm-3 task-header-asset">@if($task->multi_asset_task_id > 0) Multi-Asset Task @else {!! $task->asset->name !!} - {!! $task->asset->parent_asset->name !!}
@endif
@if(!is_null($task->crew_assignment_id)) <span class="crew text-nowrap">Crew {!! $task->crew_assignment->id !!}</span> @endif</div>
<div class="col-md-4 col-sm-3 task-header-task">{!! $task->sourceable->name !!}</div>
<div class="col-md-1 col-sm-1 task-header-priority-level text-nowrap">Priority: {!! $task->priority_level !!}</div>
<div class="col-md-1 col-sm-1 task-header-hours text-nowrap">Hours: {!! $task->hours !!}</div>
<div class="col-md-1 col-sm-1 task-header-status text-nowrap">{!! $task->status_text() !!}</div>
</div>
</a>
</div>
<div class="clear-both"></div>
<!-- Inside of panel -->
@if ($task->multi_asset_task_id > 0)
@include('partials.tasklist_accordion_multi')
@else
@include('partials.tasklist_accordion_indi')
@endif
<!-- End inside of panel -->
<?php
$i++;
$last_multi_asset_id = $task->multi_asset_task_id;
?>
@endif <!-- Ends the multi_asset if -->
@empty
@if (isset($staff))
There are no {!! $text !!} tasks for {!! $staff['first_name'] !!} {!! $staff['last_name'] !!} {!! $text2 !!}.
@else
There are no tasks.
@endif
@endforelse
</div>