【发布时间】:2023-01-28 03:13:55
【问题描述】:
我有以下查询,其中我使用 addSelect 函数添加了三个新列
DB::connection('mysql_slave')
->table('applications')
->whereNull('applications.deleted_at')
->when($column != 'contract_return_date' && $column != 'contract_delivery_date',function ($query) use ($column,$date_from,$date_to){
return $query->whereBetween('applications.'.$column, [$date_from, $date_to]);
})
->join('customers','applications.customer_id','=','customers.id')
->join('departments','applications.department_id','=','departments.id')
->select([
'applications.id',
'applications.customer_id',
DB::raw('CONCAT(IFNULL(customers.last_name,"")," ",customers.first_name ) as customers_name'),
DB::raw('CONCAT(IFNULL(applications.last_name,"")," ",applications.first_name ) as contract_name'),
'applications.offer_type as offer_type',
'applications.status_id',
'applications.contract_no',
'applications.current_provider',
'applications.extra_offer',
'applications.offer_warranty',
'applications.department_id',
'customers.mobile_phone as customer_mobile',
'applications.program as program',
'applications.saled_by_text as saler',
'departments.name as department',
'applications.created_at as created_at',
'applications.created_at as saled_at',
DB::raw('IF(applications.sale=1,"NAI","OXI") as sale'),
])
->addSelect(['submission_date'=> StatusLog::select('created_at')
->whereColumn('application_id','applications.id')
->where('status','=',1)
->latest()
->take(1)
])
->addSelect(['resubmission_date'=> StatusLog::select('created_at')
->whereColumn('application_id','applications.id')
->where('status','=',2)
->latest()
->take(1)
])
->addSelect(['error_date' => StatusLog::select('created_at')
->whereColumn('application_id','applications.id')
->whereIn('status', [5, 6])
->latest()
->take(1)
]) ->when($column == 'contract_delivery_date',function ($query) use ($date_from,$date_to){
return $query->whereBetween('submission_date', [$date_from, $date_to]);
});
上面的查询用于在数据表上打印数据。
查询包括使用 addSelect 函数添加的列,并且这些列在表中正确显示。
但是,当我尝试查询 submission_date 字段时,遇到错误:
1054 Unknown Column submission_date.
有没有办法查询在 Laravel 中使用 addSelect 函数创建的列?
感谢您的帮助,对于我的英语中的任何错误,我深表歉意。
【问题讨论】:
-
您的每个表都有“submission_date”字段?在这种情况下,设置表名。例如,$query->whereBetween('applications.submission_date', [$date_from, $date_to])
-
@bluestar0505 我试过了。它导致相同的错误
-
尝试 $query->whereBetween('submission_date.created_at', [$date_from, $date_to]);
-
哪一个数据库表有专栏
submission_date吗?它来自customers、departments或applications表吗?请澄清。