原始查询没有输出


Raw queries no output

Hi可以就这个没有输出的原始查询寻求一些帮助,

   $userproj_id = UserProject::select('projid')->where('user_id', $id)->get()->toArray();
        $flattenid = array_flatten($userproj_id);
        foreach ($flattenid as $projid) {
            $projidarr [] = $projid;
        }

  $users = DB::select('select * from project where projid in  ( ? )  and user_id = 3 ', $projidarr );

但如果我手动这样做,效果很好。。

$users = DB::select('select * from project where projid in  ( ? )  and user_id = 3 ', ['12345']);

但当我手动执行此操作时,不会再次输出。

 $users = DB::select('select * from project where projid in  ( ? )  and user_id = 3 ', ['12345','11111']);

提前谢谢。

这是因为在绑定数组中,您必须有许多参数——SQL只需要一个,而您提供了两个。一个合适的参数列表应该是[inbroide(',',[123451111])],但在这种情况下,它将是一个字符串,结果也不会是正确的

这个怎么样?

DB::table('project')
->whereIn('projid', [12345, 1111])
->where('user_id', '=', 3);

原始查询:

$users = DB::select('select * from project where projid in  ('. implode(',', $projidarr).')  and user_id = 3 ');

和Laravel查询:

DB::table('project')
->whereIn('projid', $projidarr)
->where('user_id', '=', 3);