如何显示预加载值
How to display the eager loaded value
我的预加载查询结果如下所示。我想知道如何显示特定 specValue
的值。
例如specification_id
是9,它有一个特定的值。但并非适用于所有型号。因此,此阵列仅适用于某些型号。
BrandModel {#295 ▼
#table: "models"
#searchable: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:6 [▶]
#original: array:8 [▶]
#relations: array:4 [▼
"pivot" => Pivot {#294 ▶}
"brand" => Brand {#385 ▶}
"specValues" => Collection {#2139 ▼
#items: array:19 [▼
0 => SpecValue {#2142 ▼
#table: "specification_values"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▶]
#original: array:9 [▼
"id" => 9
"specification_id" => 9
"model_id" => 1
"value" => "2,400,000 impressions"
"filtered_value" => "2,400,000"
"created_at" => "2015-12-31 16:47:11"
"updated_at" => "2015-12-31 16:40:52"
"name" => "max monthly duty cycle"
"title" => "Monthly Volume"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => SpecValue {#2143 ▼
#table: "specification_values"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 18
"specification_id" => 18
"model_id" => 1
"value" => "2,000 sheets"
"filtered_value" => "Single 2,000 Sheet Paper Drawer"
"created_at" => "2015-12-31 16:47:11"
"updated_at" => "2015-12-31 16:40:52"
"name" => "std paper capacity"
"title" => "Std Paper Capacity"
]
#original: array:9 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
2 => SpecValue {#2144 ▶}
3 => SpecValue {#2145 ▶}
现在我这样检查:
<li>Speed B/W: @foreach($model->specValues as $spec) @if($spec->name=='Monthly Volume'){{$spec->value}} CPM @endif @endforeach </li>
但这似乎不对。我怎样才能做到这一点?
我也试过这个:
{{$model->specValues()->where('specification_id', 227)->first()->filtered_value}}
但是如果模型没有规格 ID,例如 227,那么我会收到错误消息,提示我尝试访问非 属性。
使用 keyBy()
函数根据特定键组织 collection:
在blade中:
$specs = $model->specValues->keyBy('title');
@if(isset($specs['Monthly Volume'])
{{$specs['Monthly Volume']->value}}
@endif
我的预加载查询结果如下所示。我想知道如何显示特定 specValue
的值。
例如specification_id
是9,它有一个特定的值。但并非适用于所有型号。因此,此阵列仅适用于某些型号。
BrandModel {#295 ▼
#table: "models"
#searchable: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:6 [▶]
#original: array:8 [▶]
#relations: array:4 [▼
"pivot" => Pivot {#294 ▶}
"brand" => Brand {#385 ▶}
"specValues" => Collection {#2139 ▼
#items: array:19 [▼
0 => SpecValue {#2142 ▼
#table: "specification_values"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▶]
#original: array:9 [▼
"id" => 9
"specification_id" => 9
"model_id" => 1
"value" => "2,400,000 impressions"
"filtered_value" => "2,400,000"
"created_at" => "2015-12-31 16:47:11"
"updated_at" => "2015-12-31 16:40:52"
"name" => "max monthly duty cycle"
"title" => "Monthly Volume"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => SpecValue {#2143 ▼
#table: "specification_values"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 18
"specification_id" => 18
"model_id" => 1
"value" => "2,000 sheets"
"filtered_value" => "Single 2,000 Sheet Paper Drawer"
"created_at" => "2015-12-31 16:47:11"
"updated_at" => "2015-12-31 16:40:52"
"name" => "std paper capacity"
"title" => "Std Paper Capacity"
]
#original: array:9 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
2 => SpecValue {#2144 ▶}
3 => SpecValue {#2145 ▶}
现在我这样检查:
<li>Speed B/W: @foreach($model->specValues as $spec) @if($spec->name=='Monthly Volume'){{$spec->value}} CPM @endif @endforeach </li>
但这似乎不对。我怎样才能做到这一点?
我也试过这个:
{{$model->specValues()->where('specification_id', 227)->first()->filtered_value}}
但是如果模型没有规格 ID,例如 227,那么我会收到错误消息,提示我尝试访问非 属性。
使用 keyBy()
函数根据特定键组织 collection:
在blade中:
$specs = $model->specValues->keyBy('title');
@if(isset($specs['Monthly Volume'])
{{$specs['Monthly Volume']->value}}
@endif