Laravel 4.2 Flamboyant use of lists () with connection request

I have a request that uses multiple connections:

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

}

      

And I want to get an array of ID from this request in order to use it in another scope, so:

$query->purchased($userID)->lists('id')

      

My initial thought was to use lists ('id') which complained about an ambiguous request for an 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
)

      

Makes sense, fair enough, so I changed the lists to

$query->purchased($userID)->lists('characters.id')

      

Thought that table and column naming should fix it, but finding that the lists function is unsetting the character. part and so with the same error.

Looks like lists might not use dot notation, lead me to my question ... Can I avoid dot notation or is there another way to get the list of IDs as an array?

Many thanks

+3


source to share


1 answer


You can use with the column name alias lists

:

$query->purchased($userID)->select('characters.id as _id')->lists('_id');

      



This will avoid column name collisions.

+11


source







All Articles