如何在 laravel 5.2 中显示计数 (*)
How to display count(*) in laravel 5.2
收到如下回复。我需要打印 "count(*)" 字段以及一些其他数据,例如 "Duke (2)",其中 2 是计数。
如何打印 laravel 模板中的值。
{
parts_model_id: 8,
parts_id: 29,
parts_title: "duke multiple model",
model_id: 1,
model_name: "OPAH2",
created_at: "2017-07-18 17:02:10",
updated_at: "2017-07-18 17:02:10",
count(*): 2
}
代码
$models = PartsModel::with('model')->selectRaw('*, count(*)')->groupBy('model_id')->get();
谢谢
你可以这样做:
$models = PartsModel::with('model')
->selectRaw('*, count(*) AS countElements')
->groupBy('model_id')
->get();
并且您可以使用(循环后:))访问计数:
$model->countElements;
收到如下回复。我需要打印 "count(*)" 字段以及一些其他数据,例如 "Duke (2)",其中 2 是计数。 如何打印 laravel 模板中的值。
{
parts_model_id: 8,
parts_id: 29,
parts_title: "duke multiple model",
model_id: 1,
model_name: "OPAH2",
created_at: "2017-07-18 17:02:10",
updated_at: "2017-07-18 17:02:10",
count(*): 2
}
代码
$models = PartsModel::with('model')->selectRaw('*, count(*)')->groupBy('model_id')->get();
谢谢
你可以这样做:
$models = PartsModel::with('model')
->selectRaw('*, count(*) AS countElements')
->groupBy('model_id')
->get();
并且您可以使用(循环后:))访问计数:
$model->countElements;