Php 获取最多两天的前10个常用表格实体?

Php 获取最多两天的前10个常用表格实体?,php,laravel,php-carbon,Php,Laravel,Php Carbon,这是我的mySql表“listazelja”: 我想在我的控制器中获得标题中提到的代码: <?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

这是我的mySql表“listazelja”:

我想在我的控制器中获得标题中提到的代码:

<?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'));
    }
}
试试这个

$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();
$maxhours=Carbon::now()->subHours(48)->toDateTimeString();
$listaZelja=DB::table('listaZelja')
->orderBy('zapis_count','desc')
->选择(DB::raw('count(*)as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->有('created_at'take(10)->get();
试试这个

$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();
$maxhours=Carbon::now()->subHours(48)->toDateTimeString();
$listaZelja=DB::table('listaZelja')
->orderBy('zapis_count','desc')
->选择(DB::raw('count(*)as zapis_count'),'zapis_id')
->groupBy('zapis_id')
->有('created_at'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();
$twoDaysOldDate=日期(“Y-m-d”,标准时间(“现在-48小时”);
$listaZelja=DB::table('listaZelja')
->orderBy('zapis_count','desc')
->选择(DB::raw('count(*)as zapis_count'),'zapis_id')
->groupBy('zapis_id')

->whereDate('created_at',”我认为您的查询如下:

$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();
$twoDaysOldDate=日期(“Y-m-d”,标准时间(“现在-48小时”);
$listaZelja=DB::table('listaZelja')
->orderBy('zapis_count','desc')
->选择(DB::raw('count(*)as zapis_count'),'zapis_id')
->groupBy('zapis_id')

->whereDate('created_at','我认为'created_at'只是作为字符串传递,因为您在$sada.PS内调用它。这就是我讨厌框架的原因。请尝试在
Carbon::createFromFormat($format,$time,$tz)中使用Mysql NOW()函数和INTERVAL或TiMEDIFF
第二个参数必须是
$time
但您正在传递一个值为
created_at
的字符串。从
Carbon
doc
Carbon::createFromFormat('Y-m-d H',1975-05-21 22')->toDateTimeString()中提取;//1975-05-21 22:00:00
@EddyTheDove的答案似乎没有问题,我认为“created_at”只是作为字符串传递,因为您在$sada.PS内调用它。这就是我讨厌框架的原因。请尝试在
Carbon::createFromFormat($format,$time,$tz)中使用Mysql NOW()函数和INTERVAL或TiMEDIFF
第二个参数必须是
$time
,但您正在传递一个值为
created_的字符串。
Carbon
doc
Carbon::createFromFormat('Y-m-d H','1975-05-21 22')->toDateTimeString();/1975-05-21 22:00:00
@eddystedove的答案似乎没有“$”的意思在maxhours,其他东西也不起作用。但是,无论如何,thanx。:)除了在maxhours缺少“$”之外,其他东西也不起作用。但是,thanx无论如何。:)