This is my mySql table "listazelja": http://prntscr.com/duj2tf
i want to get what is mentioned in title with this code in my controller:
<?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'));
}
}
This is error I am getting:
InvalidArgumentException in Carbon.php line 425: A four digit year could not be found Data missing
I know that problem is with this line of code:
->having($sada->diffInHours(Carbon::createFromFormat('Y-m-d H-m-s', 'created_at')) <= 48)
so what should I do to make it work.
P.S. created_at in mySql is of type: timestamp
I think your query like:
$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();
Hope this work for you!