0

I have two tables

1. users (id,name, address) 1,abc,address1 2,xyz,address2 2. books (id,user_id,book,price) 1,1,book1,10 2,2,book1,10 3,1,book2,15 4,1,book3,20 

When I run Eloquent query like this

User::with('books') ->withCount('books') ->wherein('id',[1,2])->get(); 

It give me result of count each user under books

Relationship is done properly.

What I want is 1. total count of all user books 2. total sum of price for all user 

how can I do that in Eloquent

Thanks

1 Answer 1

1

You can try it like this

User::with('books') ->withCount([ 'books as total_book', 'books as total_price' => function($query) { $query->select(DB::raw('sum(price) as total_price')) } ]) ->wherein('id',[1,2]) ->get(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.