2016-02-26 90 views
0

我的加載的查詢結果如下所示。我想知道如何顯示特定的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,然後我得到的錯誤說,試圖訪問非財產。

回答

1

使用keyBy()功能基礎上的特定鍵來組織的集合:

刀片

$specs = $model->specValues->keyBy('title'); 

@if(isset($specs['Monthly Volume']) 

    {{$specs['Monthly Volume']->value}} 

@endif 
+0

如果'$規範[ '每月搜索量'] - >值;'是不存在的一個模型,然後它返回錯誤'undefined index' .. – user1012181

+0

編輯刀片檢查isset – Jeff