Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

359
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda