PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] How to make search system work in laravel which can run through an array stored as string in database?

 February 19, 2022     laravel, laravel-5, laravel-5.8, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing