这是我的 Eloquent 模型。
public function getRecordCount($id = NULL){
if ($id == 0) {
$count = $this->leftJoin('liip_user_customers', 'warehouse_to', '=', 'liip_user_customers.customer_id')
->where('status', '=', $id)
->where('created_by_id', '=', Auth::user()->id)
->select('id')
->count('id');
if (!empty($count)) {
return $count;
}else{
return 0;
}
}
}
<小时/>
这是在我的 Controller 上。
public function getIndex(){
$count_approval = $this->warehouse_transfer->getRecordCount(0);
$count_correction = $this->warehouse_transfer->getRecordCount(14);
$count_final = $this->warehouse_transfer->getRecordCount(13);
$count_pending = $this->warehouse_transfer->getRecordCount(15);
// Title
$title = Lang::get('liipWarehousePalletTransfer::warehousePalletTransfer/title.warehouse_pallet_transfers_management');
// Show the page
return View::make('liipWarehousePalletTransfer::index', compact( 'title', 'count_approval', 'count_correction', 'count_close', 'count_disapprove', 'count_final', 'count_pending'));
}
我遇到了一些错误:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id'
in field list is ambiguous (SQL: select count(`id`) as aggregate from
`liip_warehouse_pallet_transfers` left join `liip_user_customers` on
`warehouse_to` = `liip_user_customers`.`customer_id` where `status` = 0
and `created_by_id` = 159)
“待批准”选项卡中的徽章应为“5”。 我已经搜索过这个问题,但找不到与我相同的场景。
最佳答案
$this->leftJoin('liip_user_customers',
'warehouse_to', '=', 'liip_user_customers.customer_id')
发生错误的原因是您在左联接中缺少表名称TABLE_NAME.warehouse_to
参见Laravel Join了解更多信息
关于php - Laravel 4.2 Eloquent : How can i return the count of id based on the following query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48434345/