Use relationship in selectRaw
Is there a way to do something like this?
Format::with('chain')->selectRaw("id as value, concat(description, ' - ', chain.name) as label")->get()
I get this error
Column not found: 1054 Unknown column 'chain.name' in 'field list'
Eager-loading chain results in a separate query so the parent query knows nothing of the chains table.
You would need to use a JOIN to have your query working as expected:
Format::with('chain')
->join('chain', 'chain.id', 'formats.chain_id') // assumed FK???
->selectRaw("id as value, concat(description, ' - ', chain.name) as label")
->get()
You probably want to pluck('value', 'label') in place of get() as well???
@tykus How would you do it with pluck instead?
@msslgomez this will produce label => value pairs:
Format::with('chain')
->join('chain', 'chain.id', 'formats.chain_id')
->selectRaw("id as value, concat(description, ' - ', chain.name) as label")
->pluck('value', 'label')
Please or to participate in this conversation.