Issue
I wish to make a search box which will run the search query. I have stored my data in database table in this form
asdf,english class science 5,chapter 1
I wish to use, to seperate between different tags.
I need to search across this. So if the user types chapter1 the search query should be able to pick up the chapter1 in the row. At present the search query is not able to pick anything. My code is as follows:
$tag = $request->search;
$query = Comics::where('comics_tag','LIKE','%'.$tag.'%')
->get();
var_dump($query);die();
How to resolve it?
Solution
I suggest that instead of storing your tags as a string, you should create a many to many relationships between your comics and your tags, you will be able to easily manage them after :
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many
then once you have a tags()
relationship for your comics :
$comics = Comics::whereHas('tags', function($query){
$query->where('name', 'like', "%" . request()->search . "%");
})->get();
As for the naming convention, your Models should be singular, so it should be :
Comic::class
and not Comics::class
Answered By - Mathieu Ferre
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.