Skip to content
Advertisement

Multiline Eloquent query

I’m trying to filter my products based on selected filters and possibly a search term/word. My filters have a relationship with categories, which in their turn have a relation ship with my products. My code below only works (without the if statement checking for a search term/word) when everything is chained together, but when I try to break the query into multiple lines (which I’ve read is possible, right?) it returns an empty array.

Here’s a my code:

JavaScript

Advertisement

Answer

Lukas’ answer led me to do some more debugging and eventually solving my problem, though it was not the position of the if statement checking if there’s a search term/word.

The problem lies in the following line:

$products->limit(10)->get();

I needed to store the retrieved results from the get(); method in another variable, in my case:

$response = $products->limit(10)->get();

I eventually ended up with the following working code:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement