0

this my code

DB::connection('mysql2')->insert('INSERT INTO pm_booking_service (id_booking, title, qty, amount) VALUES ('2','restaurant','1','27')' );

I don't know how to do insert Using Multiple Database Connections but select is working fine

$bookings = DB::connection('mysql2')->select('select * from pm_booking');
apokryfos
  • 38,771
  • 9
  • 70
  • 114
linuxpro
  • 3
  • 1
  • Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Abdulla Nilam Sep 12 '22 at 11:44

1 Answers1

0

When you have a string that starts with a single quote you need to escape any single quotes it contains otherwise they are interpreted as closing the string.

Use parameters to not need to quote the values manually:

DB::connection('mysql2')
    ->insert('INSERT INTO pm_booking_service (id_booking, title, qty, amount) VALUES (?,?,?,?)', [2,'restaurant',1,27]);

An example of this can be found in the docs as well

This can be rewritten in the query builder as :

DB::connection('mysql2')->table('pm_booking_service')
    ->insert([ 
        'id_booking' => 2,
        'title' => 'restaurant', 
        'qty' => 1,
        'amount' => 27
    ]);

Update statements are also written similarly:

DB::update(
    'update pm_booking_service set qty = ? where id = ?',
    [100, 2]
);

This also can be written in the query builder as:

DB::connection('mysql2')->table('pm_booking_service')
    ->where('id', 2)
    ->update([ 'qty' => 100 ]);
apokryfos
  • 38,771
  • 9
  • 70
  • 114