我正在尝试使用雄辩的created_at
函数来查找列avg
的平均年龄。我可以得到平均日期,但是现在我想我只需要获取今天的日期,然后用平均年龄减去今天的日期即可。但是,我对此有疑问。我正在调用该函数,所有这些都在Axios调用中发生,但它返回一个500
错误。
这是被调用的函数:
public function ajaxRequest() {
$ticketCount = array();
$campusNames = array();
$campuses = Campus::where('TechID', Auth::user()->id)->get();
$timezone = date_default_timezone_get();
$date = date();
foreach($campuses as $campus) {
$campusNames[] = $campus->CampusName;
$tickets = Ticket::where('CampusID', $campus->id)->where('created_at', '<=', Carbon::now()->subDays(5))->where('TicketStatus','New Issue')->avg('created_at');
$averageAge = date_diff($tickets, $date );
$ticketCount[] = $tickets;
}
return(['ticketCount' => $ticketCount,
'campusNames' => $campusNames]);
}
此外,这条路线是更好的选择,还是获取每张票证的年龄,将age变量添加到数组中然后对数组取平均值,会更好吗?
编辑
这是平均年龄数组:
["20190112075852.0000",
"20190112075900.0000",
"20190112075904.0000"]
这是日志:
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'date() expects ...', 'C:\\\\Users\\\\grosal...', 65, Array)
#1 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\app\\Http\\Controllers\\PagesController.php(65): date()
#2 [internal function]: App\\Http\\Controllers\\PagesController->ajaxRequest()
#3 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): call_user_func_array(Array, Array)
#4 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('ajaxRequest', Array)
#5 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\PagesController), 'ajaxRequest')
#6 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(176): Illuminate\\Routing\\Route->runController()
#7 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(682): Illuminate\\Routing\\Route->run()
#8 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#9 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#10 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#11 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#12 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#13 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#14 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(75): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#16 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#17 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#18 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#19 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#21 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(63): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#25 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(66): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#28 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#31 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(684): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#32 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(659): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#33 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(625): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#34 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(614): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#35 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#36 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#37 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#38 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#39 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#40 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#41 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#42 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#43 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#44 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#45 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#46 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#47 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#48 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#49 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#50 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#51 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#52 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#53 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#54 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#55 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#56 C:\\Users\\grosales\\Documents\\project-falcon\\Project-Falcon\\server.php(21): require_once('C:\\\\Users\\\\grosal...')
#57 {main}
"}
我已经在date变量中添加了参数,但遗憾的是结果仍然相同
public function ajaxRequest() {
$ticketCount = array();
$campusNames = array();
$campuses = Campus::where('TechID', Auth::user()->id)->get();
$timezone = date_default_timezone_get();
$date = date('m/d/Y h:i:s a', time());
foreach($campuses as $campus) {
$campusNames[] = $campus->CampusName;
$tickets = Ticket::where('CampusID', $campus->id)->where('created_at', '<=', Carbon::now()->subDays(5))->where('TicketStatus','New Issue')->avg('created_at');
$averageAge = date_diff($tickets, $date);
$ticketCount[] = $tickets;
}
return(['ticketCount' => $ticketCount,
'campusNames' => $campusNames]);
}
答案 0 :(得分:1)
为您提供的三个提示:
n+1
queries issue结尾。类似的事情可能起作用:
class Campus extends Model
{
public function tickets()
{
return $this->hasMany(Tickets::class);
}
}
class Tickets extends Model
{
public function getAgeAttribute()
{
return $this->created_at->diffInDays(Carbon::now());
}
}
public function getTicketAverageAgePerCampus()
{
$campuses = Campus::with(['tickets' => function ($query) {
$query->where('TicketStatus', 'New Issue');
}]);
$averageTicketAges = [];
foreach ($campuses as $campus) {
$averageTicketAges[$campus->id] = $campus->tickets->average('age');
}
}