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

beli0135's avatar

Search result to a model? + Json matching count.

Hello. I have two questions that I have doubts - before I make it:

  1. if I have a search collection that is dug out from multiple tables, and that collection will be used in multiple views and some other purposes, should I leave it as a collection and toss it around, or should I make model for that search and have collection of that model?

  2. Json field in table has single word, multiple values. Search goes well with "whereJsonContains" but, how can I get which of the result has most matches? Example:

searching for ["Apples", "Oranges", "Potatoes"]

1, ["Apples", "Strawberries", "Potatoes", "Mushrooms"]
2, ["Apples", "Mushrooms"]
3, ["Apples", "Oranges", "Potatoes", "Mushrooms", "Raspberries"]

Result should be 3 - 3 matches, 1 - 2 matches, 2 - 1 match (or 3 - 100%, 1 - 66%, 2 - 33%) whatever, but you get the idea.

0 likes
1 reply
vincent15000's avatar

For your first question, if you need to share the same data with multiple views, you can create view composers. Models are really dedicated to run with a unique table (somebody can perhaps confirm this, but it's my opinion).

https://laravel.com/docs/9.x/views#view-composers

For your second question, I don't have any idea for the moment.

Please or to participate in this conversation.