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

290
Views
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 answers
Answer question

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 Report

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 Report

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 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!