简体   繁体   English

查找内部逗号分隔值字段Laravel 5.1

[英]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. 我需要使用querybuilder检查mysql字段中逗号分隔的字符串内是否存在变量。

I do this 我做这个

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

but didn't return any value. 但没有返回任何值。

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();

You may debug the query using toSql() method after your query, which becomes 您可以在查询后使用toSql()方法调试查询,该查询将变为

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. 这将清除是否在查询中放入$ categoria-> id。

I can't comment yet, hence posting it as an answer. 我无法发表评论,因此将其发布为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM