获取最多出现两天的前 10 个频繁 table 实体?
Get top 10 frequent table entites which were max two days old?
这是我的mySqltable"listazelja":
http://prntscr.com/duj2tf
我想在我的控制器中使用此代码获取标题中提到的内容:
<?php
namespace App\Http\Controllers;
use App\ListaZelja;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GlazbeniController extends Controller
{
public function listazelja(){
$sada = Carbon::now();
$listaZelja = DB::table('listazelja')->orderBy('zapis_count','desc')->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->having($sada->diffInHours(Carbon::createFromFormat('Y-m-d H-m-s', 'created_at')) <= 48)
->take(10)->get();
return view('pregledajlistuzelja',compact('listaZelja'));
}
}
这是我得到的错误:
InvalidArgumentException Carbon.php 第 425 行:
找不到四位数的年份
数据缺失
我知道这行代码有问题:
->having($sada->diffInHours(Carbon::createFromFormat('Y-m-d H-m-s', 'created_at')) <= 48)
所以我应该怎么做才能让它发挥作用。
P.S。 mySql 中的 created_at 是类型:timestamp
试试这个
$maxhours = Carbon::now()->subHours(48)->toDateTimeString();
$listaZelja = DB::table('listazelja')
->orderBy('zapis_count','desc')
->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->having('created_at' <= maxhours)
->take(10)->get();
我认为你的查询是这样的:
$twoDaysOldDate = date("Y-m-d",strtotime('now -48 hours'));
$listaZelja = DB::table('listazelja')
->orderBy('zapis_count','desc')
->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->whereDate('created_at', '<=', $twoDaysOldDate)
->take(10)
->get();
希望这对你有用!
这是我的mySqltable"listazelja": http://prntscr.com/duj2tf
我想在我的控制器中使用此代码获取标题中提到的内容:
<?php
namespace App\Http\Controllers;
use App\ListaZelja;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GlazbeniController extends Controller
{
public function listazelja(){
$sada = Carbon::now();
$listaZelja = DB::table('listazelja')->orderBy('zapis_count','desc')->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->having($sada->diffInHours(Carbon::createFromFormat('Y-m-d H-m-s', 'created_at')) <= 48)
->take(10)->get();
return view('pregledajlistuzelja',compact('listaZelja'));
}
}
这是我得到的错误:
InvalidArgumentException Carbon.php 第 425 行: 找不到四位数的年份 数据缺失
我知道这行代码有问题:
->having($sada->diffInHours(Carbon::createFromFormat('Y-m-d H-m-s', 'created_at')) <= 48)
所以我应该怎么做才能让它发挥作用。
P.S。 mySql 中的 created_at 是类型:timestamp
试试这个
$maxhours = Carbon::now()->subHours(48)->toDateTimeString();
$listaZelja = DB::table('listazelja')
->orderBy('zapis_count','desc')
->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->having('created_at' <= maxhours)
->take(10)->get();
我认为你的查询是这样的:
$twoDaysOldDate = date("Y-m-d",strtotime('now -48 hours'));
$listaZelja = DB::table('listazelja')
->orderBy('zapis_count','desc')
->select(DB::raw('count(*) as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->whereDate('created_at', '<=', $twoDaysOldDate)
->take(10)
->get();
希望这对你有用!