I am trying to insert data from a table to another but I want, when I transfer this data to other table ids to change. For example(Table1 ID=1001 -> Table2 custom_id=custom_1001). I'm trying to do it in Laravel.
This is how I transfer data from table1 to table2. But I don't now how to add 'custom_' before id on table2
$qry = DB::table('table1')->get();
foreach($qry as $key => $data_in) {
DB::table('table2')->insert([
"custom_id" => $data_in->id,
"status" => $data_in->status
]);
}
CodePudding user response:
You can use mysql CONCAT function to add prefix
$qry = DB::table('table1')
->selectRaw('CONCAT("custom_",id) AS custom_id')
->addSelect('status')
->get();
foreach($qry as $key => $data_in) {
DB::table('table2')->insert((array)$data_in);
}
CodePudding user response:
first make custom_id
string then just do something like this:
$qry = DB::table('table1')->get();
foreach($qry as $key => $data_in) {
DB::table('table2')->insert([
"custom_id" => "custom_" . $data_in->id,
"status" => $data_in->status
]);