Laravel Database Basics
Laravel making it easy to connect to the database and run the query. The application's database configuration is located in
config/database.php . In this file you can define all the database connections and specify which connection is the default connection. This file provides a sample configuration of all supported database systems.
by default, Laravel The sample environment configuration is already Laravel Homestead be prepared, of course, you can also modify the configuration for the local database as needed.
Here are some basic application examples, with comments, for reference.
Route::get ('/', function () {
query. What is the value of the array ? Laravel, which is The characteristic of the
$results = Db::select (' select * from admin where id=? ', [1]);
Update
$results = db::update (' update admin set name =? where id =? ', [' Laravel ', 1]);
Insert
$results = Db::insert (' INSERT into admin (name,password) VALUES (?,?) ', [' Laravel2 ', 123]);
Delete
$results = DB::d elete (' delete from admin where id =? ', [102]);
BULK Insert
$results = db::table (' admin ')->insert ([
[' Name ' = ' laravel3 ', ' Password ' =>1],
[' Name ' = ' laravel4 ', ' Password ' =>2],
[' Name ' = ' laravel5 ', ' Password ' =>3],
[' Name ' = ' laravel6 ', ' Password ' =>4],
// ]);
return ID after insert
$results = db::table (' admin ')->insertgetid ([
' Name ' = ' laravel7 ', ' Password ' =>123
// ]);
update a specific ID
$results = db::table (' admin ')->where (' id ', 109)->update ([
' Name ' = ' update name ', ' Password ' =>123
// ]);
Delete a specific ID
$results = db::table (' admin ')->where (' id ', 108)->delete ();
return only name
$results = db::table (' admin ')->get ([' name ']);
returns only the first record
$results = db::table (' admin ')->first ();
a record with a query ID greater than
$results = db::table (' admin ')->where (' id ', ' > ')->get ();
Records with a query ID greater than or less than 5
$results =db::table (' admin ')->where (' id ', ' > ', ' + ')->orwhere (' id ', ' < ', 5)->get ();
query ID from 104 to record (contains both and 104)
$results =db::table (' admin ')->wherebetween (' id ', [100,104])->get ();
query only records with ID 100,102,105
$results = db::table (' admin ')->wherein (' id ', [100,102,105])->get ();
only query ID other than 100,102,105 records
$results =db::table (' admin ')->wherenotin (' id ', [100,102,105])->get ();
Query only 3 ID Records
$results =db::table (' admin ')->where (' id ', ' > ', +)->take (3)->get ();
Query only 3 records
$results =db::table (' admin ')->where (' id ', ' > ', +)->limit (3)->get ();
Remove 3 records after skipping a record
$results =db::table (' admin ')->where (' id ', ' > ', +)->skip (1)->take (3)->get ();
only name with ID is removed
$results = db::table (' admin ')->where (' id ', "->pluck") (' name ');
See how many records there are
$results = db::table (' admin ')->count ();
See what the maximum ID is
$results = db::table (' admin ')->max (' id ');
See what the minimum ID is
$results = db::table (' admin ')->min (' id ');
See what the average ID is
$results = db::table (' admin ')->avg (' id ');
See What the sum of IDs is
$results = db::table (' admin ')->sum (' id ');
DD ($results);
});
This article from the "Good Learning Day Up" blog, please be sure to keep this source http://sunrising.blog.51cto.com/10729461/1697026
Laravel Database Basics