Jun 21, 2017
0
Level 8
Using Query Parameter Bindings with whereRaw in Scope
I'm using a query scope and I'm getting 0 results when I should be getting 1 result for my query.
On the Eloquent model...
public function scopeHashtagSearch($query, $value)
{
return $query->whereRaw('json_contains(hashtags, \'["?"]\')', [$value]);
}
In tinker...
>>> $query = App\Models\Forums\Post::hashtagSearch('matchit')->toSql();
=> "select * from `forum_posts` where json_contains(hashtags, '["?"]')"
>>> $query = App\Models\Forums\Post::hashtagSearch('matchit')->getBindings();
=> [
"matchit",
]
>>> $query = App\Models\Forums\Post::hashtagSearch('matchit')->get();
=> Illuminate\Database\Eloquent\Collection {#1424
all: [],
}
But when I run select * from forum_posts where json_contains(hashtags, '["matchit"]') directly in MySQL it returns 1 result (which is what I'm expecting).
What's wrong with my query scope and bindings? Why is Eloquent not returning the matched record?
Please or to participate in this conversation.