Change and delete
After you have configured the database connection, you can use the DB class to query.
Inquire
$results = Db::select (' select * from users where id =? ', array (1));
The Select method returns the result of the query in the form of an array.
Insert
Db::insert (' INSERT into users (ID, name) VALUES (?,?) ', Array (1, ' Dayle '));
Update
Db::update (' update users set votes = + WHERE name =? ', Array (' John ') ');
Delete
DB::d elete (' Delete from users ');
Note: The update and DELETE statements return the number of rows affected.
General statements
Db::statement (' drop table users ');
Listening to the query event
Db::listen (function ($sql, $bindings, $time) { //});
Database transactions
Db::transaction (function () { db::table (' users ')->update (Array (' votes ' = 1)]; Db::table (' posts ')->delete ();});
Note: Any exception in the transaction closure will cause the transaction to be automatically rolled back.
Sometimes you might want to start your own business:
Db::begintransaction ();
You can also roll back the transaction yourself:
Db::rollback ();
Finally, you can commit the transaction through the Commit method:
Db::commit ();
Multiple Database connection access
When you use multiple database connections, you can access them through the Db::connection method:
$users = db::connection (' foo ')->select (...);
You can access the native connection via the PDO instance:
$pdo = Db::connection ()->getpdo ();
Sometimes you need to re-connect the database:
Db::reconnect (' foo ');
If you want to disconnect because you exceed the maximum number of connections for the PDO instance, use the Disconnect method:
DB::d isconnect (' foo ');
Query log
By default, Laravel uses logging to log all database query operations that are currently requested, but some times, such as inserting large amounts of data, can cause an application to use excessive memory. In order to close the log, you can use the Disablequery method:
Db::connection ()->disablequerylog ();
To get query logging, you can use the Getquery method:
$queries = Db::getquerylog ();
Reference: http://nonfu.me/p/10911.html, Http://nonfu.me/job
Laravel Framework MySQL database-Basic use