I have a collection that contain data like
{
"_id": {
"$oid": "610b5c978e82a1738333dfc1"
},
"title": "What is Lorem Ipsum?",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
"body": "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, Unknown printer galley of type and scrambled it to make a type specimen book."
}
{
"_id": {
"$oid": "610b5fa2c96a6958dc654881"
},
"title": "Where does it come from? book",
"description": "It was popularised in the 1960s with the release of Letraset sheets book",
"body": "Contrary to popular belief, Lorem Ipsum random text. It has roots in a piece of classical Latin literature from 45 BC making it over 2000 years old. Book"
}
{
"_id": {
"$oid": "610b5fa2c96a6958dc654881"
},
"title": "Why do we use it? Magic",
"description": "It is a long established fact that a reader will be distracted. Magic",
"body": "The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using, making it look like readable English. Magic"
}
{
"_id": {
"$oid": "610b5fa7c96a6958dc654884"
},
"title": "Magic Where can I get some?",
"description": "Magic There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration",
"body": "If you are going to use magic Lorem Ipsum, you need to be sure there isn't anything hidden in the middle of text. All the Lorem Ipsum generators on the Internet"
}
I want to search for a word and find in all table but my problem I don’t know how to order them by the object that has the word used in more fields
Example if I search for book the first result should be the second object because the second object has the word book in title and description and in body so he should be the first one. If I search for Magic the first result should be the 3rd and the 4th object because the last two object has Magic in title, description and body
I mean the 1st will be the object that has the word in all fields the second will be the one with the word in two fields and last one will be the object that has the word in only one fields
I tried my code like :
$query->where(['or', ['like', 'title', $key ], ['like', 'description', $key ], ['like', 'body', $key ]])->all();