Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Omda's avatar
Level 1

I have two db's I want to transfare content of post table to other table called act_post

I have two databases and they are at the same server and I want to transfer the content of one table to another table in the second database

0 likes
8 replies
Sinnbeck's avatar

Using command line, a scheduled task or something else?

Omda's avatar
Level 1

@sinnbeck thank for your answer how can implement this in laravel as you see there is one .env fil ?

Sinnbeck's avatar

Can you give some context? Do you just need to run this once? Or like every day?

Sinnbeck's avatar

A bit to get you started. Set the connection on each model to the respective database

protected $connection = 'mysql1';

And do something like this

$data = Model1::all(['col1', 'col2'] );
Model2::insert($data->toArray);
Omda's avatar
Level 1

This my post table on the first database

    Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('post_name');
            $table->text('post_content');
            $table->timestamp();
        });

This my buss_post table on the second database

    Schema::create('buss_posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('post_name');
            $table->text('post_content');
            $table->timestamp();
        });

and this my controller BlogController

class BlogController extends Controller
{
    public function run()
    {

        $oldPosts = \DB::table('buss_posts')->get();
      
            
    }
}

so my question how to changes between two databases to access tables inside it.

Sinnbeck's avatar

Set up 2 connections with different databases. (database.php)

Sinnbeck's avatar

The difference is that their code inserts one line at the time and mine inserts all (only 2 queries)

Please or to participate in this conversation.