我怎样才能从 Laravel Illuminate\Support\Collection 得到 属性?

How can I get property from Laravel Illuminate\Support\Collection?

我正在尝试 属性 从集合中,但我做不到。

我有这个查询:

$calls = collect(                                                                                                                                                                                                                                                                      
   DB::select("SELECT count(id)                                                                                                                                                                                                                                                               
               FROM calls                                                                                                                                                                                                                                                                     
               WHERE started_at >= '" . $date . "' AND                                                                                                                                                                                                                                               
                     linked_id IS NULL")                                                                                                                                                                                                                                          );

结果我得到了这个

Illuminate\Support\Collection {#82146
     all: [
       {#1097
         +"count": 15,
       },
     ],
   }

我怎样才能得到这个"count"?我试过

$calls->count
$count = get_object_vars($calls)

但在第一种情况下我有: PHP 错误:未定义 属性:第 1 行 Illuminate\Support\Collection::$count 在第二个我得到空数组。

First, Please give alias to "count(id) as calls_count"

这是您的查询。

$calls = collect(DB::select("SELECT count(id) as calls_count FROM calls WHERE started_at >= '" . $date . "' AND linked_id IS NULL"));

现在有几种获取计数值的方法。

1.)

echo $calls[0]->calls_count;

2.)

$calls = $calls->first();
echo $calls->calls_count;

3.)

echo $calls->pluck('calls_count')->first() 

(我更喜欢这个)

希望对您有所帮助,如有任何疑问,请告诉我评论。