rubenSousa
rubenSousa

Reputation: 291

Find inside comma separated values field Laravel 5.1

I Need to check if a variable is present inside a comma separated string in mysql field using querybuilder.

I do this

 <?php 
      $parents = DB::table('categorie')>whereRaw('FIND_IN_SET("$categoria->id", parent)')->get();                            

but didn't return any value.

Upvotes: 1

Views: 1848

Answers (2)

Jerodev
Jerodev

Reputation: 33196

You should never put variables in the query yourself. Use bindings instead, this will make sure your parameters are correctly escaped.

<?php 
    $parents = DB::table('categorie')->whereRaw('FIND_IN_SET(?, parent)', [$categoria->id])->get();

Upvotes: 5

Himanshu Sharma
Himanshu Sharma

Reputation: 456

You may debug the query using toSql() method after your query, which becomes

DB::table('categorie')->whereRaw('FIND_IN_SET("$categoria->id", parent)')->toSql();

This will clear it out if $categoria->id is being put in the query or not.

I can't comment yet, hence posting it as an answer.

Upvotes: 0

Related Questions