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

292
Vistas
Count when a user order a product

I want make count when a user login and make order about some product This is my table

  • User : Id(1 to many->order), name, email, address, phone

  • order : Id, id_user, id_product, date, price, total_order, total_price

I have written

$count = order::where('id_user','Auth->user()')->count();

Code above does not work, if I write like this

$count = order::all()->count();

It work but count all the data even i login with different user

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You have to try this its working properly

$count = order::where('id_user',auth()->user()->id)->count();

OR

$count = order::where('id_user',auth()->id())->count();
over 4 years ago · Santiago Trujillo Denunciar

0

'Auth->user()' firstly you tried a string not the id of logged user, you have to remove the quotation marks and add the use Auth; to import this class and then use it.

use Auth;

$count = order::where('id_user', Auth::user()->id)->count();

over 4 years ago · Santiago Trujillo Denunciar

0

Make a relation from the user model

public function orders(){
    return $this->hasMany(Order::class, 'parent_id', 'id');
}

Then you can call wherever you want like this

auth()->user()->orders->count()
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