M
M
maaestr02022-03-29 19:24:11
Laravel
maaestr0, 2022-03-29 19:24:11

How to use the withAvg ,withExists, methods?

Hello, I have a method that gets the average score, the presence of a discount on a product

$courses = Product::all()
                            ->withAvg('reviews', 'rating')
                            ->withExists('discount')                
                            ->get();

There is the following method, which gets all the purchased products of the current user.
$purchasedProducts = auth()->user()->courses;
But how to apply the withAvg and withExists methods to this method?
I will be grateful for advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2022-03-29
@alexey-m-ukolov

Product::query() instead of Product::all().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question