T
T
turindd2021-08-17 11:26:17
Laravel
turindd, 2021-08-17 11:26:17

How to add parameters to eager loading?

Hello.
I have a eager loading request with(['filter_value')
How to forcefully add an array with parameters here, for example, you need to include it in filter_value with id = [111,222,333].
That is, I first filtered out the parameters where there are no products so that they are not shown to the user ( because there are no such products with such parameters), and now you need to add these parameters forcibly with id = 111, id = 222, id = 333,

Thank you in advance!

$filter = Filter::where('on_off', 1)
    ->whereHas('category_filter', function ($query) use ($category_one) {
        $query->where('category_id', $category_one->id);
    })
    ->whereHas('product_filter', function ($query) use ($product) {
        $query->whereIn('product_id', $product->pluck('id'));
    })
    ->with(['filter_value' => function ($query) use ($product) {        // жадная загрузка 'filter_value'
        $query->whereHas('product_filter', function ($query) use ($product) {
            $query->where('product_id', $product->pluck('id'));
        });
        //$query->where('id', 116);
    }])
    ->get()
    ->sortBy('id');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Tokarev, 2021-08-26
@Vadiok

$filter = Filter::where('on_off', 1)
    // ...
    ->with(['filter_value' => function ($query) use ($product) {
        $query
            ->where(function ($q) use ($product) {
                $query->whereHas('product_filter', function ($query) use ($product) {
                    $query->where('product_id', $product->getKey());
                });
            })
            ->orWhereIn('id', [111,222,333]);
    }])
    //...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question