How to Move Laravel Database ?
ALI KARABAY laraveldatabase transferdatabase migrationdatabase 0 Comment

Hi friends in this article we will learn how to move the data from our databases to another database

First mysql Connection database/config.phpcopy and paste under your file, then edit the connection names as follows

'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

'mysql-transfer' => [
'driver' => 'mysql',
'host' => env('DB_TRANSFER_HOST', '127.0.0.1'),
'port' => env('DB_TRANSFER_PORT', '3306'),
'database' => env('DB_TRANSFER_DATABASE', 'forge'),
'username' => env('DB_TRANSFER_USERNAME', 'forge'),
'password' => env('DB_TRANSFER_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Then open your file in your .env and configure the database link as follows

DB_CONNECTION=mysql-transfer

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=

DB_TRANSFER_HOST=127.0.0.1
DB_TRANSFER_PORT=3306
DB_TRANSFER_DATABASE=database2
DB_TRANSFER_USERNAME=root
DB_TRANSFER_PASSWORD=

Now let's add a new seeder to your Seeds folder.

php artisan make:seed TransferSeeder

Now let's open this file and add the following to the working function. table_nametype the name of the table where you will move your data


use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class TransferSeeder extends Seeder
{
/**
* Run the database seeds.
*
*
@return void
*/
public function run()
{
// burada veri tabanı bağlanıyoruz
$database = DB::connection('mysql');
// burada ise taşıyacağımız verileri alıyoruz
// not : blog yazan yere taşıyacağımız verilerin tablosunu yazıyoruz
foreach ($database->table('blog')->get() as $data) {
// Buradada table_name yazan yere taşıyacağımız tablonun ismini yazıyoruz

DB::table('table_name')->insert((array) $data);
}
}
}

When you're done, you can use the following code DatabaseSeeder.php just add it to your file.

public function run()
{
$this->call(TransferSeeder::class);
}

Run this command after you add the above code

php artisan migrate:fresh --seed

Note: If the fields in the table you are moving are not the same as the fields in the other table, php artisan migrate:fresh --seed you also receive an error in the command that you are running.

But if the fields between the two tables are going to be different TransferSeeder.php we open our file and fix it as follows.


use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class TransferSeeder extends Seeder
{
/**
* Run the database seeds.
*
*
@return void
*/
public function run()
{
// burada veri tabanı bağlanıyoruz
$database = DB::connection('mysql');
// burada ise taşıyacağımız verileri alıyoruz
// not : blog yazan yere taşıyacağımız verilerin tablosunu yazıyoruz
foreach ($database->table('blog')->get() as $data) {
// Buradada table_name yazan yere taşıyacağımız tablonun ismini yazıyoruz ve taşıyacağımız tablonun alanlarını aşağıdaki şekilde düzenliyoruz ve $data değişkeninden gelen verileri onlara aktarıyoruz

DB::table('table_name')->insert((array) [
'id' => $data->id,
'title' => $data->baslik,
'content' => $data->icerik,
]);
}
}
}

After we fix it in this way, we run the following code and all our data is moved to that table

php artisan migrate:fresh --seed

  Quote

KARABAY A, 2021 . How to Move Laravel Database?,

https://www.karabayyazilim.com/blog/php/framework/laravel/laravel-veritabani-tasima-islemi-nasil-yapilir-2020-05-27-143117

(Accessed May 27, 2020).


  Share this post

Comments (0)

Comment

Subscribe
Sign up for the email newsletter to be the first to know about my blog posts