Skip to content
Advertisement

CONCAT columns with Laravel 5 eloquent

Consider me as laravel beginner

The goal is: I have two colums, now I need the id to be prefixed with the component name of same row in the table.

For Example (Working)… I have Mysql like

JavaScript

And output is

ID

JavaScript

I need the same in laravel eloquent way, as here Component is Model name. So i tried something like

JavaScript

but it doesn’t work.
I think because the syntax is wrong.
Kindly help me with the correct syntax. Using laravel Models.

Note: I made the above query, referring this as which available on internet.

JavaScript

Advertisement

Answer

You need to wrap your query in DB::raw:

JavaScript

Also, note because you are doing your query like this, your model might behave differently, because this select removes all other fields from the select statement. So you can’t read the other fields from your model without a new query. So ONLY use this for READING data and not MODIFYING data.

Also, to make it in a nice list, I suggest you modify your query to:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement