Reputation: 33
I have second database in Laravel. I want to insert data to second database and I search topic where as an example is given:
$users = DB::connection('mysql2')->select(...);
How can I insert in the same way?
Upvotes: 0
Views: 2315
Reputation: 106
The insert method accepts an array of column names and values:
DB::table('mysql2')->insert(
['email' => '[email protected]', 'votes' => 0]
);
Don't forget to use 'use Illuminate\Support\Facades\DB;' at the beginning
Upvotes: 0
Reputation: 68
Pass an array of fields and values to the insert function like so:
DB::connection('mysql2')->table('your_table')->insert(
['email' => '[email protected]', 'name' => 'John Smith']
);
Upvotes: 2