简体   繁体   English

如何使用 laravel 迁移将数据从一个表传输到另一个数据库表

[英]How can i transfer data from one table to another database table using laravel migration

I am trying hard to get transfer data of one database table to another database table我正在努力将一个数据库表的数据传输到另一个数据库表

Via Laravel migration, I have raw database queries like below通过 Laravel 迁移,我有如下原始数据库查询

insert into users 
    ( temp_foreign_id, aadhar_card, created_at, date_of_birth, deleted, email, email_verified, name, pan_number, passport_no, password, phone, phone_verified, profile_photo_url, type, updated_at )
    ( select partners.id, null, partners.created, null, partners.deleted, partners.email, partners.email_verified, partners.name, null, null, partners.password, partners.phone, partners.phone_verified, JSON_EXTRACT ( partners.meta, '$.image' ), 'partner', partners.updated from hookfish.partners )");

I was trying to put it in migration file and run it like below我试图把它放在迁移文件中并像下面一样运行它

   <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class UsersForPartners extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            
        DB::statement(DB::raw('SET @prev=0,@rownum=0;'));
        $result=DB::raw("insert into users 
        ( temp_foreign_id, aadhar_card, created_at, date_of_birth, deleted, email, 

email_verified, name, pan_number, passport_no, password, phone, phone_verified, profile_photo_url, type, updated_at ) ( select partners.id, null, partners.created, null, partners.deleted, partners.email, partners.email_verified, partners.name, null, null, partners.password, partners.phone, partners.phone_verified, JSON_EXTRACT ( partners.meta, '$.image' ), 'partner', partners.updated from hookfish.partners )"); } email_verified, name, pan_number, passport_no, password, phone, phone_verified, profile_photo_url, type, updated_at ) ( select partners.id, null, partners.created, null, partners.deleted, partners.email, partners.email_verified, partners.name, null,null,partners.password,partners.phone,partners.phone_verified,JSON_EXTRACT(partners.meta,'$.image'),'partner',partners.updated from hookfish.partners)");}

Thanks for any help i can get from you all感谢您提供的任何帮助

hey guys I got an answer to my question as I was going in the wrong way to run database raw queries in migration.嘿伙计们,我得到了我的问题的答案,因为我在迁移中以错误的方式运行数据库原始查询。 I was trying DB::raw("") it was supposed to be DB::insert("query inside this") for inserting data from one schema to another and DB::select("raw query here") to select values from the table though I used DB::insert("") I got my data inserted from one table to another database table.我正在尝试 DB::raw("") 它应该是 DB::insert("query inside this") 用于将数据从一个模式插入到另一个模式和 DB::select("raw query here") 到 select 值尽管我使用了 DB::insert("") 从表中获取数据,但我将数据从一个表插入到另一个数据库表。

$result=DB::insert("insert into users 
        ( temp_foreign_id, aadhar_card, created_at, date_of_birth, deleted, email, email_verified, name, pan_number, passport_no, password, phone, phone_verified, profile_photo_url, type, updated_at )
        ( select raw_partners.id, raw_partners.aadhar_number, raw_partners.created, null, raw_partners.deleted, raw_partners.email, 0, raw_partners.name, raw_partners.pan_number, null, null, raw_partners.phone, 0, null, 'rera_partner', raw_partners.updated from hookfish.raw_partners )
        ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 laravel 将一个表的内容传输到另一个数据库表 - How can I transfer content of one table to another database table using laravel 如何使用Database:Seeding将数据从一个表导入另一个表? (laravel 5.3)? - How to import data from one table to another table using Database: Seeding? (laravel 5.3)? 如何从 Laravel 中的另一个数据库表中获取一个表的值 - How can i get one table's value from a another table of Database in Laravel 如何使用Laravel 4将数据从一个表链接到另一个表? - How to link data from one table to another using Laravel 4? 插入时将数据从一个表转移到另一个表 - Transfer data from one table to another on insert 如何在 laravel 中使用不同的迁移再添加一张表? - How can I add one more table using different migration in laravel? 如何将一行从一个表转移到另一个表? - How to transfer a row from one table to another? PHP SQL:我无法将数据从一个表传输到数据库 sql 中的用户界面按钮 sql 文件中的 UI 按钮 - PHP SQL: I cant transfer data from 1 table to another table in database sql using UI button in php file 将数据从表传输到同一数据库中的另一个表 - Transfer data from table to another table in same database 是否可以将表数据从一个表转移到另一个表? - is it possible to transfer table data from one table to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM