1

I'm trying to bind vars to my raw query,

$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
          FROM category_translations
          JOIN categories ON categories.id = category_translations.category_id
          WHERE name LIKE '%?%'";

$results = DB::select($query, ['ger']);

But I am unable to do so. I have tried by binding named params, and also using DB::raw inside DB::select without any success.

What am I doing wrong here?

2 Answers 2

2

You need to add the %'s to the bind value and not in the query (unless you concat() the values)...

$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
          FROM category_translations
          JOIN categories ON categories.id = category_translations.category_id
          WHERE name LIKE ?";

$results = DB::select($query, ['%ger%']);
Sign up to request clarification or add additional context in comments.

Comments

1

try to used like that

$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
          FROM category_translations
          JOIN categories ON categories.id = category_translations.category_id
          WHERE name LIKE '% :nameSearch %'";

$results = DB::select(DB::raw($query), ['nameSearch' => 'ger']);

second way used like that

$searchText = "ger";
$results = DB::select(DB::raw($query), ['nameSearch' => "%".$searchText."%"]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.