简体   繁体   English

where 子句中的列“department_id”不明确

[英]Column 'department_id' in where clause is ambiguous

I got error on my page like the title above.我的页面上有错误,如上面的标题。 I am trying to export an Excel with the Laravel Excel extension.我正在尝试使用Laravel Excel扩展名导出 Excel

Here is my code:这是我的代码:

public function query()
    {
        $test =  Leave::query()
                ->join('departments as dep', 'leaves.department_id', '=', 'dep.id')
                ->join('employees as emp', 'leaves.employee_id', '=', 'emp.id')
                ->join('users as emplUser', 'emp.user_id', '=', 'emplUser.id')
                ->join('users as apprUser', 'leaves.approved_by_id', '=', 'apprUser.id')
                ->select('leaves.id',
                        'dep.name',
                        'emplUser.first_name',
                        'leaves.start',
                        'leaves.end',
                        'leaves.type',
                        'leaves.reason',
                        'leaves.approved',
                        'leaves.approved_on',
                        'apprUser.first_name',
                        'leaves.approved_comment',
                        'leaves.created_at',
                        'leaves.updated_at',
                        )
                ->whereDate('leaves.start','>=', $this->periodStart)
                ->whereDate('leaves.end', '<=', $this->periodEnd);
        return $test;
    }

and here is the SQL from the error message:这是错误消息中的 SQL:

select
  `leaves`.`id`,
  `dep`.`name`,
  `emplUser`.`first_name`,
  `leaves`.`start`,
  `leaves`.`end`,
  `leaves`.`type`,
  `leaves`.`reason`,
  `leaves`.`approved`,
  `leaves`.`approved_on`,
  `apprUser`.`first_name`, 
  `leaves`.`approved_comment`,
  `leaves`.`created_at`,
  `leaves`.`updated_at`
from `leaves` 
  inner join `departments` as `dep` on `leaves`.`department_id` = `dep`.`id`
  inner join `employees` as `emp` on `leaves`.`employee_id` = `emp`.`id`
  inner join `users` as `emplUser` on `emp`.`user_id` = `emplUser`.`id`
  inner join `users` as `apprUser` on `leaves`.`approved_by_id` = `apprUser`.`id`
where date(`leaves`.`start`) >= 2021-07-04 and date(`leaves`.`end`) <= 2021-12-31
  and (`department_id` = 2 or `department_id` is null)
  order by `leaves`.`id` asc limit 1000 offset 0

I have notice that it says:我注意到它说:

where ... and (`department_id` = 2 or `department_id` is null) But I have never specified department_id, just like the start and end date. where ... and (`department_id` = 2 or `department_id` is null)但是我从来没有指定department_id,就像开始和结束日期一样。 I think it needs like leaves .我认为它需要像leaves一样。 department_id , but how can I do that when I have never write it from the first time? department_id ,但是当我从第一次开始从未写过它时,我怎么能做到这一点?

Update with more code:更新更多代码:

This is from the LeaveController:这是来自 LeaveController:

    public function export() 
    {
        $now = Carbon::now()->startOfWeek(Carbon::SUNDAY);
        $start = $now;
        $end = $now->copy()->endOfYear();
        
        $period = new Period($start, $end);
        return (new LeavesExport)->forPeriod($period->start, $period->end)->download('download.xlsx');
    }

This is some of the code from Leave, that I found that contains department in some way:这是 Leave 的一些代码,我发现它以某种方式包含部门:

use App\Traits\HasDepartment;

* App\Leave
 * @property int $department_id
 * @property-read \App\Department $department
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Leave whereDepartmentId( $value )

class Leave extends Model
{
    use HasDepartment, ...

public static function getTypes()
   {
        try {
            return LeaveType::where('department_id', current_department()->id)->pluck('name', 'id');
        } catch (\Exception $e) {
            error_log('User id: ' . auth()->user()->id . ' does not have an assigned Department');
            return collect([]);
        }
    }
 }


The error in your WHERE clause is ambiguous means that the system is not able to indentify department_id because there are more than 1 column with that name. WHERE 子句中的错误是不明确的,这意味着系统无法识别 Department_id,因为具有该名称的列超过 1 个。 You need to specify it first.您需要先指定它。

return LeaveType::where('leaves.department_id', current_department()->id)->pluck('name', 'id');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内部连接错误 - where 子句中的列“id”不明确 - Inner join error - Column 'id' in where clause is ambiguous Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 在 where 子句中加入两个表列 id 不明确 - Laravel joining two tables column id in where clause ambiguous Laravel Nova列中where子句不明确/ where子句中的未知列 - Laravel Nova Column in where clause is ambiguous/Unknown column in where clause Laravel - 认证返回“where子句中的列&#39;id&#39;是不明确的”因为GlobalScope中的JOIN - Laravel - Authentication returns “Column 'id' in where clause is ambiguous” because a JOIN in a GlobalScope 违反完整性约束:1052 where子句不明确的列&#39;prof_id&#39;Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel 如何解决完整性约束违规:1052 where子句中的&#39;agent_id&#39;列不明确 - How to solve Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 如何解决完整性约束违规:1052 列 &#39;id&#39; in where 子句在 laravel 中不明确 - How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列 'id' 不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM