Hi everyone,
I have 2 models :
-
card: a simple model
-
cardlist: a custom list created by a user
The relation is many-to-many.
i'm trying to get all cardlist of the user, and check if every cardlist has a specified card. To do that, I want to use a subqurey, but it's not working via Eloquent.
Here is my code:
$query = Cardlist::addSelect(['has_card' => function($q) use ($card) {
$q->select('id')
->from('cards_in_cardlists')
->where('card_id', $card)
->whereColumn('list_id', 'cardlists.id')
->limit(1)
->exists()
;
}])
->get()
;
And my error:
Column not found: 1054 Unknown column 'cardlists.id' in 'where clause' (SQL: select exists(select * from cards_in_cardlists where card_id = 19 and list_id = cardlists.id limit 1) as exists)
This query works perfectly:
select `cardlists`.*, (
select exists(
select `id`
from `cards_in_cardlists`
where `card_id` = 19
and `list_id` = `cardlists`.`id`
limit 1
) as `exists`
) as `has_card`
from `cardlists`
So maybe I make a mistake using Eloquent, but I don't know where.
Can someone help me? Thank you :)