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

harit's avatar
Level 1

Binding params with Raw Query

I have a query for a paginated listing page. Some part of the SELECT and WHERE parts are raw queries. To avoid sql vularabilities, I need to use parameter binding for this query. It is worked fine for WhereRawquery but not worked in SELECTsection.

Please see my query here:

$aJob  = JobKeywords::where('job_keywords.status', 1)
                            ->where('job_keywords.expiry_date', '>=', $today);
               whereRaw('MATCH(job_title,company_name,skills) AGAINST (? IN BOOLEAN MODE) > 0', [$keyword])
          ->select([DB::Raw('job_keywords.job_id as job_id, ? as candidate_id, 1 as fts_search', [$candidateId]),"job_keywords.location as job_location_name",
            DB::raw('MATCH(job_title,company_name,skills) AGAINST (? IN BOOLEAN MODE) as score', [$keyword]),
            "job_keywords.company_name",
            "job_keywords.job_id as id","job_keywords.job_title",
             "job_keywords.skills",
             "job_keywords.expiry_date",
            "job_keywords.monthly_salary_range_min","job_keywords.monthly_salary_range_max",
            'job_keywords.min_experience_in_year',
            'job_keywords.metadata'])
    ->orderBy('score', 'DESC')
    ->paginate(10)

The parameter binding in WhereRawis working fine but inside Select(DB::Raw) not working. It will throw SQLSTATE[HY093]: Invalid parameter number error. If we dubug print_r($aJob->toSql()) and print_r($aJob->getBindings()) then you can see the parameter inside DB::raw is not counted.

Do any one have any idea about this?

Thanks in advance

0 likes
2 replies
harit's avatar
Level 1

That worked. thank you :)

Please or to participate in this conversation.