I am trying to figure out how to create an Eloquent request dynamically using user input, which modifies the request depending on the length of the input. Using simple MySQL, it's just ... just concatenating the query string based on user input. Using Eloquent is not possible unless you use a) a raw SQL query that defeats the purpose of using Eloquent or b) Use something very unhealthy like the eval () function.
How can you iterate over user input of unknown length and create an Eloquent query that searches for multiple fields for words using a combination of "OR" and "AND"
Consider the following pseudo code that cannot work.
$search = "cat sat on the mat";
$searchWords = explode(' ', $search);
$data['finds'] = DB::table('mytable') [... incomplete query]
foreach ($searchWords as $word) {
->where('firstname', 'like', '%' . $word . '%')
->orWhere('lastname', 'like', '%' . $word . '%')
->orWhere('address', 'like', '%' . $word . '%')->AND [... next word]
}
? , PHP / , , MySQL.
MySQL , , :
SELECT * FROM `mytable`
WHERE (firstame LIKE '%$cat%' OR lastname LIKE '%cat%' OR address LIKE '%cat%')
AND (firstname LIKE '%sat% OR lastname LIKE '%sat%' OR address LIKE '%sat%')
AND [etc, etc, for all words in user input]