Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
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

over 4 years ago ยท Santiago Trujillo
2 answers
Answer question

0

$q = Visitor::all(); returns a Illuminate\Database\Eloquent\Collection.

Calling $visitors = $q->orderBy('created_at', 'desc')->paginate(15); will call ->orderBy(...) on an Eloquent Collection of which that method doesn't exist in that class.

Solution

Instead of:

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

Use this:

$q = Visitor::query(); โœ…
over 4 years ago ยท Santiago Trujillo Report

0

Maybe you can use ->whereDate('created_at', now()), with ->whereTime('created_at', '00:00:00') if you need

over 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!