Contents of this article
1. DB operation database (emphasis)
According to the MVC architecture, the operation of the data should be done in the Model, but if we do not use the Model, we can also use the DB class provided by the laravel framework to operate the database, and for some extremely complex SQL, it is difficult to use the Model. To complete, the developer needs to write the SQL statement by himself and use the DB class to execute the native SQL. The basic usage of the DB class in laravel DB::table['tableName']
obtains tableName
the instance (object) of the operation table.
1.1, the creation and configuration of the data table
Ways to build data:
- sql statement
- Graphical interface phpMyAdmin Navicat
Create a database:
create a table:
create table member(
id int primary key auto_increment,
name varchar(32) not null,
age tinyint unsigned not null,
email varchar(32) not null
) engine myisam charset utf8;
1.2, the configuration of the database in the laravel framework
In the .env
file
It can also be configured in the database.php file under the config directory. Using the env function means to get it from the env file first, if it is successful, use it, if it fails, use the second parameter of the env function.
1.3. Introduce the DB facade in the Test controller
1.3.1. Define the routes needed for addition, deletion, modification, and check:
Increase: /add
delete: /del
modify: /mod
query: /select
1. Adding information (insert)
There are two main functions for adding data to a table in the database, which are insert()
andinsertGetId()
insert (array) can add one or more at the same time, the return value is Boolean type.
insertGetId (one-dimensional array), only one piece of data can be added, and the self-increasing id is returned.
Grammar: DB::table('表名')->insert();
Coherent operation/chain operation
Example:
Effect:
2. Modify the data (update)
Data modification can be implemented using update(), increment() and decrement() methods.
The update method means that all the fields in the entire record can be modified.
increment and decrement means to modify the value of a numeric field (increment or decrement). Typical applications: record the number of logins and increase in points;where() -> update([])
Note: where() -> where() -> where()... This syntax is and (and) relational syntax.
where() -> orWhere() -> orWhere()... This grammar is an or (or) relational grammar.
The parameters of the orWhere method are the same as where.
Where parameter order:
where (field name, operator, field value). For example, if id=1, you can write: where('id','=', 1), abbreviated as Where('id', '1'); [only = can be abbreviated]
Example: Rename the name with id=1 to'lisi' (the return value indicates the number of rows affected):
Effect:
Example: add 10 to the age field value of the user with id=1
DB::table(‘member’) -> where(‘id’, ‘=’, 1) -> increment(‘age’); 每次+1
DB::table(‘member’) -> where(‘id’, ‘=’, 1) -> increment(‘age’, 5); 每次+5
DB::table(‘member’) -> where(‘id’, ‘=’, 1) -> decrement(‘age’); 每次-1
DB::table(‘member’) -> where(‘id’, ‘=’, 1) -> decrement(‘age’, 5); 每次-5
3. Query data (get)
(1) Take out the basic data
Example: Get all the data in the member table
DB::table('member') -> get(); // 相当于select * from member
; the
return value is a collection object, the
effect:
Example: Take out the data
with id less than 3 Effect:
Example: Query id> 2 AND age <25 data
native sql statement: select * from member where id > 2 and age <25
;
Laravel statement:
Effect:
(2) Take out a single row of data. The
DB::table('member') -> where('id', '1') -> first()
return value is an object.
The first method is equivalent to limit 1
Note: the difference between first and get, the return value of first is an object, and even if get has only 1 record, it is also a collection result set.
Summary scenario:
use first scenario:
use first scenario: login verification, details page , Modify functions, etc.
Scenarios where get is used: list pages, design interfaces, etc.
Example:
Effect:
(3) Get a specific value (a field)
DB::table('member') -> where('id', '1') -> value('name')
;
Example: query the name of the user whose id is 1 (just want the name)
Effect:
(4) Get some field data (multiple fields)
$users = DB::table('member') -> select('name', 'email') -> get();
$users = DB::table('member') -> select('name as user_name') -> get();
$users = DB::table('member') -> select(DB::raw('name, age')) -> get(); // 不解析字段,原样使用。
Example: Get the name and email field in the record with id>2. Native SQL: select name, email from member where id > 2
;
effect:
(5) Sort operation
DB::table('member') -> orderBy('age', 'desc') -> get()
;
syntax: orderBy (sort field, sort rule). desc descending order, asc ascending order
Example: Query the list, and it is required to sort in descending order according to the age field.
effect:
(6) a paging operation
DB::table('member') -> limit(3) -> offset(2) -> get()
;
limit
: represents the number of output limit (the number of records per tab)
offset
: where to begin
Example:
effects:
(7) Delete data (Delete)
in the deletion in two ways: physically deleted (The essence is to delete), logical deletion (the essence is to modify)
data deletion can be achieved through the delete function and the truncate function.
delete means to delete records;
truncate means to empty the entire data table;
DB::table('member') -> where('id', '1') -> delete()
;
effect:
(8) Execute any SQl statement
1. Execute native query statements
DB::select('select 语句')
;
2. Execute native insert statements
DB::insert('insert 语句')
;
3. Execute native modification statement
DB::update('update 语句')
4. Execute the native delete statement
DB::delete('delete 语句')
;
5. Execute a general statement (statement without return value, such as create table, etc.)
DB::statement('statement')
On the way of learning php, if you think this article is helpful to you, then please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.