【问题标题】:Raw queries no output原始查询无输出
【发布时间】:2016-04-13 18:44:42
【问题描述】:

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

   $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']);

提前谢谢你。

【问题讨论】:

标签: php laravel-5.2


【解决方案1】:

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

这个怎么样?

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

【讨论】:

    【解决方案2】:

    原始查询:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2014-03-06
      • 2019-04-27
      • 2019-08-17
      • 2018-07-04
      • 2021-10-23
      相关资源
      最近更新 更多