How i get column while compare json encoded data in select query? - laravel

The users is json encoded
$agent_id = $request->get('user');
$model = AgentPermission::select('module_category')->where('users', 'like', '%$agent_id%')->get();
I want to to show "module category" where json encoded data is present in "users" column
if it is working the whole code is working fine
because when i use this
$model = AgentPermission::select('module_category')->where('users', 'like', '%"10"%')->get(); it is working perfect

The comparison should be inside double quotes, not single quotes
$model = AgentPermission::select('module_category')->where('users', 'like', "%$agent_id%")->get();
since PHP injects variables only when using double quotes.
The alternative is:
$model = AgentPermission::select('module_category')->where('users', 'like', '%' . $agent_id . '%')->get();

Related

A way to determine if "where like" matches 100% with a post in Laravel?

Is there a way to determine if this query:
$news_result = Post::with('image')
->has('image')
->with('tags')
->where('title', 'LIKE', '%' . $search . '%')
->get();
Has a 100% match with some title? If a user searches for "Some random news title" and in the database there is a post with such title to return some kind of marker?
The LIKE operator is used to partially match something.
You should not use it, and do ->where('title', $search) instead.
Remove '%' in your code
$news_result = Post::with('image')->has('image')->with('tags')->where('title', $search)->get();

Why, request return symbol — '?'

Why any value in a variable $search return a character — ??
$search = 'words'
Post::where("description", 'like', "%".$search."%")->toSql();
Result:
"select * from `post` where `description` like ?"
The method toSql() does not include bindings, and therefore you're seeing the question mark in their place. To see the bindings, you can use getBindings() like so:
$search = 'words';
Post::where("description", 'like', "%".$search."%")->getBindings();

Laravel Eloquent Where Query with multiple words

I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.

query returning wrong result for like'%'

like % not working . the query returning result for full string match not the substring match.
Booking::whereHas('agent', function ($query) use ($agent_name) {
$query->where('first_name', 'like', "'%".$agent_name."%'");
})->select('id','agent_id','file_number','title','first_name','last_name','ref_number','pax_adult','pax_child')->with(array('agent'=>function($query){
$query->select('id','first_name','last_name');
}))->get();
Update your query to:
$query->where('first_name', 'like', "%".$agent_name."%");
You had a extra ' at the start and end of the LIKE.

full text search in codeigniter

I have a query to search keywords using like, but I also want to search full text so I changed it to full text search query, but it doesn't work.
The old query that's working fine:
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','content.category = categories.id')
->like('content.title', $searchterm)
->like('content.body', $searchterm)
->order_by("content.id","DESC")
->get();
and this is my new query for full text search:
$data = $this->db
->select('content.title,content.id,content.category,content.body,path')
->from('content')
->join('categories','content.category = categories.id')
->where('MATCH (content.body, content.title) AGAINST ("'. $searchterm .'")')
->order_by("content.id","DESC")
->get();
if you are using mysql version 5.5 or lower, make sure all the tables involved have the engine MyISAM.
make sure the your column has the FULLTEXT INDEX.
where() takes 3 arguments, example:
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
$this->db->get('table');
more than one columns in match() triggers Error Number: 1191, so separate them:
->where('MATCH (content.title) AGAINST ("'. $searchterm .'")')
->where('MATCH (content.body) AGAINST ("'. $searchterm .'")')
Try by changing the where clause in your query, see if it helps:
->where('MATCH (content.body, content.title) AGAINST ("'. $searchterm .'")', NULL, false)
This sets the value to NULL and tells CI not to escape the string.
you did not use the correct syntax of mathch .... against
try this:
->where MATCH (content.body, content.title) AGAINST ("'. $searchterm .'") > 0
if you have multiple column you can use them in single line like this:
->where("MATCH(title, model) AGAINST('$text')", null, false);
or just one column:
->where("MATCH(title) AGAINST('$text')", null, false);
don't forget to escape your inputs. because we disabled escaping with that false over there. use this for escaping:
$text = $this->db->escape_str($text);

Resources