Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ianmcqueen's avatar

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?

0 likes
0 replies

Please or to participate in this conversation.