Robert Nicjoo
Robert Nicjoo

Reputation: 365

Get user info from another table in laravel

I have Order table where user orders will be stored with their id and also have status of 0 / 1 to show if that order is paid by user or no.

Now I want to show in their profile that for example they have 10 orders and 3 paid.

I can get that number of total orders which is 10 but i have problem with getting the number of those 3 orders that has been paid.

Currently I have this:

$ordersd = DB::table('orders')->where('status', Auth::user()->username)->pluck('status')->count();

But it is not what I need cause I need to specified that count for me only orders with status 1 which belongs to logged user.

How do I do that?

Thanks.

Upvotes: 0

Views: 1380

Answers (2)

Sagar Gautam
Sagar Gautam

Reputation: 9369

Why are you comparing status with Auth::user()->username inside where condition ?

Does it makes any sense ?

$ordersd = DB::table('orders')->where('status', Auth::user()->username)
               ->pluck('status')->count();

So, change it to

$ordersd = DB::table('orders')
            ->where('user_id',Auth::user()->id)
            ->where('status', 1)
            ->count();

Hope you understand.

Upvotes: 3

Chris Baldwin
Chris Baldwin

Reputation: 589

You should be able to do this to get the count of the paid orders:

$ordersd = DB::table('orders')
             ->where([ [ 'status', '=', 1 ], 
                       [ 'user_id', '=', Auth::user()->id]
                    ])
             ->count();

To get the total number of orders:

$ordersd = DB::table('orders')
             ->where('user_id', Auth::user()->id)
             ->count();

Upvotes: 0

Related Questions