xbiu
xbiu

Reputation: 33

Laravel insert using query builder

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

Answers (2)

Elvis Sylejmani
Elvis Sylejmani

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

Two Piers
Two Piers

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

Related Questions