Laravel 5.4查询可在sql服务器上正常运行,但不能通过laravel运行Elequent Query Builder我在做什么错?

时间:2018-12-19 18:41:53

标签: mysql laravel eloquent laravel-5.4

我正在使用Laravel 5.4。我写的是Laravel雄辩的查询。当我手动将查询写入MySQL服务器时,它可以正常工作,但是当我通过Laravel Eloquent编写相同的查询时,Tt无法工作。

请帮助...

Laravel代码

$dateS = new Carbon($req->fromDate);
$dateE = new Carbon($req->toDate);

$tasks = DB::table('taskday')->selectRaw("taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total")
            ->join("assignedtask","assignedtask.fk_dayID","taskday.dayID")
            ->join("userdetails","userdetails.userID","taskday.fk_userID")
            ->whereBetween("taskday.task_date", [$dateS->format('Y-m-d')." 00:00:00", $dateE->format('Y-m-d')." 23:59:59"]);
if($req->staffname > 0){
    $tasks = $tasks->where('taskday.fk_userID',$req->staffname);
}

$tasks = $tasks->groupBy("assignedtask.fk_dayID")->get();

return $tasks;  

我的SQL查询

select `taskday`.`dayID`, `taskday`.`task_date`, `taskday`.`fk_userID`, `userdetails`.`fullname`, count(`assignedtask`.`fk_dayID`) as total 
from `taskday` inner join `assignedtask` on `assignedtask`.`fk_dayID` = `taskday`.`dayID` 
inner join `userdetails` on `userdetails`.`userID` = `taskday`.`fk_userID` 
where `taskday`.`task_date` between '2018-11-01 00:00:00' and '2018-12-19 23:59:59' 
group by `assignedtask`.`fk_dayID`

直接在mysql上进行火灾查询后,它给出正确的结果

dayID    task_date   fk_userID   fullname    total
-------- ----------  ----------  ----------  -------------
DAY021   2018-12-02  USR300920   Manish      2
DAY022   2018-12-03  USR300924   Rovio       3

Laravel给出了错误

(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1055 'medical.taskday.dayID'
 isn't in GROUP BY 
(SQL: select taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total 
from `taskday` inner join `assignedtask` on `assignedtask`.`fk_dayID` = `taskday`.`dayID` 
inner join `userdetails` on `userdetails`.`userID` = `taskday`.`fk_userID` 
where `taskday`.`task_date` 
between 2018-11-01 00:00:00 and 2018-12-19 23:59:59 
group by `assignedtask`.`fk_dayID`)

请帮助

2 个答案:

答案 0 :(得分:0)

如果我理解您的问题,请尝试此操作。

控制器

$dateS = new Carbon($req->fromDate);
$dateE = new Carbon($req->toDate);

$tasks = DB::table('taskday')
    ->where('taskday.task_date')
    ->selectRaw("taskday.dayID, taskday.task_date, taskday.fk_userID, userdetails.fullname, count(assignedtask.fk_dayID) as total")
    ->join('assignedtask.fk_dayID', 'assignedtask', '=', 'taskday.dayID')
    ->join('userdetails.userID', 'userdetails', '=', 'taskday.fk_userID')
    ->whereBetween('taskday.task_date', [date('Y-m-d 00:00:00', strtotime($dateS)), date('Y-m-d 23:59:59', strtotime($dateE))])
    ->groupBy('assignedtask.fk_dayID')
    ->get();

if($req->staffname > 0){
    $tasks = $tasks->where('taskday.fk_userID',$req->staffname);
}

$tasks = $tasks->groupBy("assignedtask.fk_dayID")->get();

return $tasks;  

请检查数据库中的时间戳格式,必须与此相同 或尝试对“ whereBetween”查询使用Unixtime格式,以进行更准确的计算。

查看

. . .
   <tr>
     <th>dayID</th>
     <th>task_date</th>
     <th>fk_userID</th>
     <th>fullname</th>
     <th>total</t>
   </tr>
@foreach($tasks as $task)
   </tr>
     <td>$task->dayID</td>
     <td>$task->task_date</td>
     <td>$task->fk_userID</td>
     <td>$task->fullname</td>
     <td>$task->total</td>
   </tr>
@endforeach
. . .

为了编码的和平:)

答案 1 :(得分:0)

这个问题实际上是针对缓存提出的。清除所有缓存后,它开始。