Laravel-违反完整性约束:1052 Laravel过滤器中where子句不明确的列“ created_at”

时间:2019-06-21 09:54:15

标签: laravel

我正在使用Laravel 5.8按日期和名称进行过滤。当我点击搜索时,出现此错误:

  

“ SQLSTATE [23000]:违反完整性约束:1052 where子句中的列'created_at'不明确(SQL:从services内部联接cloudsubscriptions的{​​{1 }}。services = namecloudsubscriptions其中service_name在2019-05-31和2019-06-29之间,而created_at在{Learnersway% DATE(cloudsubscriptions.created_at),nameservicesidservices)◀“

这是我的代码:

name

2 个答案:

答案 0 :(得分:1)

在您的代码中,您没有指定要过滤的表created_at,所以问题就来了。

要解决此问题,您需要使用表名指定一个created_at列。

更新为以下代码,它将起作用。

public function revenueReport(Request $request)
{
$data['title'] = 'Revenue Report';

$revenuereports = DB::table('cloudsubscriptions as c')
 ->select(
       'c.service_name', 
        DB::raw('COUNT(*) as total_users'), 
        DB::raw('SUM(c.amount) as total_amount'),
        DB::raw('DATE(c.created_at)as subscription_date') 
  ) 
 ->groupBy(DB::raw('DATE(c.created_at)'), 'service_name')                                                          
 ->orderByRaw('c.created_at DESC');

    $render=[];
    if(isset($request->start_date) && isset($request->end_date))
    {
        $revenuereports=$revenuereports->whereBetween('subscription_date',[$request->start_date,$request->end_date]);
        //$revenuedetails=$revenuedetails->whereBetween(date('users.created_at'),[$request->start_date,$request->end_date]);
        $render['start_date']=$request->start_date;
        $render['end_date']=$request->end_date;
    }elseif(isset($request->start_date))
    {
        $revenuereports=$revenuereports->where('subscription_date',$request->start_date);

        $render['start_date']=$request->start_date;
    }        
    if(isset($request->service_name))
    {
        $revenuereports=$revenuereports->where('service_name','like','%'.$request->service_name.'%');
        $render['service_name']=$request->service_name;
    }

    //dd($revenuedetails->toSql());
    $revenuereports= $revenuereports->orderBy('subscription_date','DESC');
    $revenuereports= $revenuereports->paginate(15);
    $revenuereports= $revenuereports->appends($render);
    $data['revenuereports'] = $revenuereports;

return view('report.revenueReport',$data);
} 

答案 1 :(得分:1)

指定从哪个表(例如c.created_at或users.created_at