Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

291
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda