Laravel 4 Laravel 4.2将lists()与联接查询结合使用
我有一个使用多个联接的查询:Laravel 4 Laravel 4.2将lists()与联接查询结合使用,laravel-4,eloquent,Laravel 4,Eloquent,我有一个使用多个联接的查询: public function scopePurchased($query, $userId) { return $query ->join('products','characters.id','=','products.productable_id') ->join('bundle_product','bundle_product.product_id','=','produc
public function scopePurchased($query, $userId)
{
return $query
->join('products','characters.id','=','products.productable_id')
->join('bundle_product','bundle_product.product_id','=','products.id')
->join('bundles','bundles.id','=','bundle_product.bundle_id')
->join('purchases','purchases.bundle_id','=','bundles.id')
->join('users','purchases.user_id','=','users.id')
->whereNull('purchases.deleted_at')
->where('purchases.refunded', false)
->where('products.productable_type', '=', get_class($this))
->where('users.id','=',$userId)
->groupBy('characters.id')
->orderBy('characters.title', 'ASC');
}
我想从这个查询中检索一个ID数组,以便在另一个作用域中使用,因此:
$query->purchased($userID)->lists('id')
我最初的想法是使用列表(“id”),它抱怨id上的查询不明确
Column 'id' in field list is ambiguous
(
SQL: select `id` from `characters`
inner join `products` on `characters`.`id` = `products`.`productable_id`
inner join `bundle_product` on `bundle_product`.`product_id` = `products`.`id`
inner join `bundles` on `bundles`.`id` = `bundle_product`.`bundle_id`
inner join `purchases` on `purchases`.`bundle_id` = `bundles`.`id`
inner join `users` on `purchases`.`user_id` = `users`.`id`
where `characters`.`deleted_at` is null
and `purchases`.`deleted_at` is null
and `purchases`.`refunded` = 0
and `products`.`productable_type` = Character and `users`.`id` = 1
group by `characters`.`id`
order by `characters`.`title` asc
)
有道理,很公平,所以我把名单改成
$query->purchased($userID)->lists('characters.id')
认为命名表和列应该可以解决这个问题,但发现lists函数删除了“character.”部分,因此产生了相同的错误
看起来列表可能不使用点符号,让我来回答我的问题。。。我可以不使用点符号吗?或者有没有其他方法可以将ID列表作为数组来获取
非常感谢您可以在使用
列表之前为列名添加别名
:
$query->purchased($userID)->select('characters.id as _id')->lists('_id');
这将避免任何列名冲突。先生,这是最棒的。这是一个简单的解决方案。非常感谢。在Laravel 5中仍然是一个问题,这仍然是解决方案。