insert..." and "DB::insert('insert into admins (name, email) values...") in Laravel 5?" />
B
B
BonBon Slick2016-08-14 13:19:15
Laravel
BonBon Slick, 2016-08-14 13:19:15

What is the difference between "DB::table('users')->insert..." and "DB::insert('insert into admins (name, email) values...") in Laravel 5?

Here is from the documentation (standard examples)

DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10).'@gmail.com',
            'password' => bcrypt('secret'),
        ]);

This is when generating autocomplete Laravel 5
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle'])
What is the difference?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2016-08-14
@BonBonSlick

In the first case, laravel will collect the request itself, in the second case, you write the request with pens.
Practical meaning: when writing queries with your hands, you can implement your own complex sql query logic.

N
Nik Gubin, 2016-08-16
@gubin_niko

Dmitry Entelis answered absolutely right, but I would add. Another difference is that if you want to change the DBMS, then you do not have to manually change all your queries, and Laravel will independently assemble the query for the new one. Those. it's a compatibility issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question