P
P
photosho2016-02-27 20:52:42
Laravel
photosho, 2016-02-27 20:52:42

How to implement partial search by string?

Hello. There was suddenly a need to make in one of the projects a page for displaying articles by certain tags. Tags are passed in the search string using the GET parameter and look like this:

site/topics?tags=0,3,5

That is, their numerical values. That is how they are in the database. I do the search like this:
Topic::where('tags', 'LIKE', '%' . $tags . '%')->get(...)...;

Now about the problem. If I search for articles by tags 0,5, and the database contains a string for the article 0,3,5(that is, there is still a value between 0 and 5), then this article does not fall into the final selection. That is, the search goes on the full line. How to implement a partial search on a string so that MySQL takes into account all the tags included in it, regardless of their order?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2016-02-27
@photosho


1. Or change a column in the database to json and the minimum then will be mysql 5.5 PHP side, after that - select content for output based on the selected article IDs.

S
Stanislav Pochepko, 2016-02-27
@DJZT

Your tags are stored incorrectly. It is necessary to create a 3rd table to store the correspondence of the article and tags. And store tags in a separate table. Then flexibility increases and such a problem does not arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question