Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdallahSabri's avatar

combine (like + in) inside where query

I have a posts table (Post model), I want to search for a string like this (Laravel query builder), and I want to search to run like the following:

1- explode search string to create array

$keys = ['Laravel', 'query', 'builder'];

2- search if any item in the array is found in post title or post body

$posts = Post::where('title', 'in', $keys)->orWhere('body', 'in', $keys)->get();

$posts return an empty array, I don't know if the previous eloquent statement has any thing wrong

0 likes
1 reply
AbdallahSabri's avatar
AbdallahSabri
OP
Best Answer
Level 1

I found the solution, Its very simple, I just removed 'in' and use

I just added '%' before and after all keys

$posts = Post::where('title', 'like', $keys)->('body', 'like', $keys)->get();

Please or to participate in this conversation.