Laravel 查询生成器中左连接的参数绑定与数组

问题描述 投票:0回答:1

我尝试使用 where IN 子句搜索进行左连接。但我无法将数组绑定到查询。

 $query = DB::table('offers');
        $query->select('id', 'business_id', 'address_id', 'title', 'details', 'value', 'total_available', 'start_date', 'end_date', 'terms', 'type', 'coupon_code', 'is_barcode_available', 'is_exclusive', 'userinformations_id', 'is_used');
        $query->leftJoin('user_offer_collection', function ($join)
        {
            $join->on('user_offer_collection.offers_id', '=', 'offers.id');
            $join->on('user_offer_collection.user_id', 'IN', DB::Raw('?'));
        })->setBindings(array_merge($query->getBindings() , array(array(
            1,2,3
        ))));
php laravel laravel-4
1个回答
1
投票

如果您使用查询构建器或雄辩的 ORM,则不必绑定参数。但是,如果您使用

DB::raw()
,请确保绑定参数。

尝试以下操作:

$array = [1, 2, 3];

$query = DB::table('offers');
$query->select('id', 'business_id', 'address_id', 'title', 'details', 'value', 'total_available', 'start_date', 'end_date', 'terms', 'type', 'coupon_code', 'is_barcode_available', 'is_exclusive', 'userinformations_id', 'is_used');
$query->leftJoin('user_offer_collection', function ($join) use ($array) {
    $join->on('user_offer_collection.offers_id', '=', 'offers.id')
         ->whereIn('user_offer_collection.user_id', $array);
});
$result = $query->get();

证明此建议有效:PHPize 演示

© www.soinside.com 2019 - 2024. All rights reserved.