如何显示预先加载的值

时间:2016-02-26 23:22:35

标签: laravel eloquent laravel-5.2

我急切加载的查询结果如下所示。我想知道如何显示特定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 个答案:

答案 0 :(得分:1)

使用keyBy()功能根据特定密钥组织集合:

刀片中的

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

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

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

@endif
相关问题