簡體   English   中英

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

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

我的頁面上有錯誤,如上面的標題。 我正在嘗試使用Laravel Excel擴展名導出 Excel

這是我的代碼:

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;
    }

這是錯誤消息中的 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

我注意到它說:

where ... and (`department_id` = 2 or `department_id` is null)但是我從來沒有指定department_id,就像開始和結束日期一樣。 我認為它需要像leaves一樣。 department_id ,但是當我從第一次開始從未寫過它時,我怎么能做到這一點?

更新更多代碼:

這是來自 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');
    }

這是 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([]);
        }
    }
 }


WHERE 子句中的錯誤是不明確的,這意味着系統無法識別 Department_id,因為具有該名稱的列超過 1 個。 您需要先指定它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM