php - Laravel 4.2 Eloquent : How can i return the count of id based on the following query?

标签 php mysql laravel-4.2

这是我的 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)

Check the attached image

“待批准”选项卡中的徽章应为“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/

相关文章:

php - 如何用 eloquent "with"选择特定的列

laravel - Laravel-获取没有表列名的数据库结果

php - 如何配置 XAMPP 从 localhost 发送邮件?

javascript - 在 WordPress 模板上包含 jQuery 脚本

mysql - 无法创建/写入文件 - Linux 中的错误代码 13

mysql - SQL 首先显示 A 列的结果,然后显示 B 列的结果

php - 使用 PHP 检查 MYSQL 上的 TIME 字段

mysql - laravel4.2 将参数传递给电子邮件 Blade

php - 如何使用 DOMDocument 获取标签内容?

PHP时差类返回不同时差