Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fynbos's avatar

Complex query (with ifnull )

Hi,

3 tables:

books [id,…]

captions [ id, caption, language]

captionables [id, captionable_type, captionable_id, caption_id]

Polymorphic relation is defined in Book and Caption models.

Each book must have a caption in the default language (eg en) and can have - or not - a caption into other languages (eg fr, es). I'm trying to write a query that returns for a given language, the list of all the books with caption in the requested language, either with the caption in the default language if the translation does not exist.

Any help much appreciated, thanks!

0 likes
0 replies

Please or to participate in this conversation.