Laravel替换查询不起作用

问题描述:

我试图通过证实获得两列的区别。但是这个查询给我错误'(daily_attendances.in_time - $time) AS profit'这个部分。如果没有这部分查询工作正常。可能是什么问题?Laravel替换查询不起作用

$time = "07:15:00"; 

$transportCounts = DB::table('daily_attendances') 
      ->join('emp_personal_details', 'daily_attendances.emp_id', '=', 'emp_personal_details.id') 
      ->select('emp_personal_details.code','daily_attendances.in_time','(daily_attendances.in_time - $time) AS profit') 
      ->where('daily_attendances.in_time', '>', $time) 
      ->where('daily_attendances.date', '=', date("Y-m-d")) 
      ->get(); 
+0

什么是实际的错误? – FMashiro

+0

@FMashiro它说没有找到列。这是发生错误的行。 '(daily_attendances.in_time - $ time)AS profit' – colombo

您使用此单引号... '(daily_attendances.in_time - $time) AS profit'

所以你的情况,你告诉MySQL来选择零下名为$time列与daily_attendances.in_time列值。

PHP将只解释双引号字符串中的变量。

http://php.net/manual/en/language.types.string.php#language.types.string.parsing

当用双引号或者定界符, 会被解析变量指定的字符串。

你需要更新你的代码,如:

$transportCounts = DB::table('daily_attendances') 
       ->join('emp_personal_details', 'daily_attendances.emp_id', '=', 'emp_personal_details.id') 
       ->select('emp_personal_details.code','daily_attendances.in_time',DB::raw('(daily_attendances.in_time - $time) AS profit')) 
       ->where('daily_attendances.in_time', '>', $time) 
       ->where('daily_attendances.date', '=', date("Y-m-d")) 
       ->get();