mercredi 16 mars 2022

How can I query all, orderby, and paginate?

enter image description here

I don't know what is wrong with my query, but the count is clearly wrong, it kept showing as 1912 when my total records are 12363


I tried

to change this line under my default case

$q = Visitor::where('created_at', '<', now());

to

$q = Visitor::all(); but I got error; 

Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.


switch ($interval) {
    case 'day':
    $q = Visitor::where('created_at', '>', now()->today());
    break;
    case 'week':
    $q = Visitor::where('created_at', '>', now()->subWeek());
    break;
    case 'month':
    $q = Visitor::where('created_at', '>', now()->subMonth());
    break;
    case 'year':
    $q = Visitor::where('created_at', '>', now()->subYear());
    break;
    case 'last':
    $q = Visitor::where('created_at', '>', $last);
    break;
    default:
    $q = Visitor::where('created_at', '<', now());
    break;
}


$visitors = $q->orderBy('created_at', 'desc')->paginate(15);

http://app.test/visitor?interval=year

I got 1912 - correct đŸ‘đŸœ


http://app.test/visitor

I got 1912 - not correct ❌, It should query all since the interval is not specified.

If I do :

dd(count(Visitor::all()));

I got 12363 so that should be the right value.

Please let me know if you spot what I missed



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire