Laravel:为什么count()少了一个

时间:2016-10-13 03:26:24

标签: laravel

为什么数组中有5个项目,count()输出4?

public function show($id)
    {
        $task = Task::findOrFail($id);

        dd($task->toArray(), $task->count());

        return view('pages.show')->with('task', $task);
    }

输出:

array:5 [▼
  "id" => 1
  "title" => "first"
  "description" => "description for first"
  "created_at" => "2016-10-09 19:34:04"
  "updated_at" => "2016-10-09 19:34:04"
]

4

阵列清楚地显示了5个项目

1 个答案:

答案 0 :(得分:1)

这是因为$task->count()将返回您Tasks表上的tasks个数(通过对数据库进行计数查询),而不是您拥有的列数。

尝试使用:count($taks->toArray())。它将返回5.