简体   繁体   中英

Laravel WhereIn method in Eloquent not working

I want to use the WhereIn method in Eloquent but it now works like the below function.

<?php 
  $product = DB::table('product')
                   ->join('supplier','supp_code','=','prod_supplier_code')
                   ->select('product.*','supplier.supp_margin')
                   ->where('prod_seo_title','=',$id)
                   ->first();

  $decArr = explode(',',$product->prod_decoration_type);

        for($i=0; $i<count($decArr); $i++)
        {
            if($i==0)
            $inString = "'".$decArr[$i]."','";
            elseif ($i<(count($decArr))-1)
            $inString .= $decArr[$i]."','";
            else
            $inString .= $decArr[$i]."'";
        }

        $proDeco = DB::table('decoration')->whereIn('deco_print_type', [$inString])->get();
?>

But In the query, it's displaying like this.

select * from `decoration` where `deco_print_type` in ('\'100109C9\',\'100110B9\',\'100144C9\',\'100186C9\'');

I can't understand why these slashes are coming. Please help me to fix this issue.

The whereIn() method will accept an array of all the items. In your example you are passing an array with one element with all the values already concatenated. Based on your example you only need to pass $decArr

DB::table('decoration')->whereIn('deco_print_type', $decArr)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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