This code work as send to inn_db table from ext_db.
but it cannot check if the data is the same or different in inn_db.
So there posited same values in inn_db.
How could I add that job?.
Laravel-5.4, MySQL, InnoDB.
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
class UpdateCustomerController extends Controller
{
public function db_update()
{
$customers = DB::connection('ext_db')->table('customers')->orderBy('customer_id')->chunk(1000, function ($all){
foreach ($all as $kunde){
DB::connection('inn_db')->table('custoemrs')->insert(
[$kunde->customer_id
$kunde->name,
$kunde->email]);
}
});
}
}
Finally, I got the answer as below code with connect view when after discussion.
Thanks to @Pramid and @Badea 🙂
$customers = DB::connection('ext_db')->table('customers')->orderBy('customer_id')->chunk(1000, function ($all){
foreach ($all as $kunde){
$existing_kunde = DB::connection('inn_db')->table('customers')->where([
['customer_id', '=', $kunde->customer_id],
['name', '=', $kunde->name],
['email', '=', $kunde->email]
])->first();
if ( ! $existing_kunde) {
DB::connection('inn_db')->table('customers')->insert([
'customer_id' => $kunde->customer_id,
'name', => $kunde->name,
'email', => $kunde->email
]);
}
}
});
$kundes = DB::connection('ext_db')->table('customers')->get();
return view('kundes.index')
->with('kundes', $kundes);
Advertisement
Answer
Give this a try, you basically need to check each record of the chunk in your customer table if it doesn’t exist then allow them to get insert into customer table
public function db_update()
{
$customers = DB::connection( 'ext_db' )->table( 'customers' )->orderBy( 'customer_id' )->chunk( 1000, function ( $all ) {
foreach ( $all as $kunde ) {
$kunde_exist = DB::connection( 'inn_db' )->table( 'customers' )
->where( [
'customer_id' => $kunde->customer_id,
'name' => $kunde->name,
'email' => $kunde->email,
] )->first();
if ( ! $kunde_exists ) {
DB::connection( 'inn_db' )->table( 'customers' )->insert(
[ $kunde->customer_id
$kunde->name,
$kunde->email]);
}
}
} );
}