首页 > 解决方案 > 如何在 laravel 集合中计数?

问题描述

这是我得到的收藏:

 1 => App\Match_fact {#1332 ▼
      #guarded: array:2 [▶]
      #connection: "mysql"
      #table: "match_facts"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:1 [▼
        "goals" => 3
      ]
      #original: array:1 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
  ]
}

我尝试了一个选项-> count($playerFacts->goals));但它对我不起作用。我收到错误: 此集合实例上不存在属性 [目标]。

如何计算从集合中获得的所有值?

标签: laravelmodel-view-controllercollectionseloquentlaravel-query-builder

解决方案


然后 $playerFacts 是集合。

count($playerFacts);

推荐阅读