Skip to content
Advertisement

Laravel Migration: Combine multiple values in a new colum

i’m trying to make a migration using Laravel. The idea is to get the values of columns ‘code’ and ‘id’ in different tables, and merge them into a column ‘name’ with ‘-‘ separators. So in part it is a SQL problem.

Currently i’m trying something like this.

First migration to create the new column ‘name’

(works just fine)

And second migration to populate the new column with values

and i’m getting this error

I’m thinking that i’m not getting the values but the index instead.

UPDATE: I solved the problem by replacing + for ||. But now in column ‘name’ all my values are “1-1-1”. The values are not being represented. What am i missing?

UPDATE2: I noticed that i was defining an unnecessary variable $work_order_number. As it belongs to the same table to be updated. So I removed it and put the field “id” directly in the statement.

Now the third value is shown correctly. And this reduces my problem to getting values from another table into an column.

Advertisement

Answer

First of all, you shouldn’t use migrations to populate the database. Read the documentation about seeds.

Also, i think your select query is wrong. Isn’t you missing a where clause?

Run php artisan migrate to migrate your database, then create a new seed, and add this to the run method:

Now, just run php artisan db:seed --class=YourSeedClassName and it should work.

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