Laravel's DB database operation

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 tableNamethe instance (object) of the operation table.

1.1, the creation and configuration of the data table

Ways to build data:

  1. sql statement
  2. Graphical interface phpMyAdmin Navicat

Create a database:
Insert picture description here
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;

Insert picture description here


1.2, the configuration of the database in the laravel framework

In the .envfile

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

Insert picture description here

Insert picture description here

1.3.1. Define the routes needed for addition, deletion, modification, and check:

Increase: /add
delete: /del
modify: /mod
query: /select

Insert picture description here
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:
Insert picture description here
Effect:
Insert picture description here
Insert picture description here


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):
Insert picture description here
Effect:
Insert picture description here
Insert picture description here


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
Insert picture description here
effect:
Insert picture description here


Example: Take out the data
Insert picture description here
with id less than 3 Effect:
Insert picture description here


Example: Query id> 2 AND age <25 data
native sql statement: select * from member where id > 2 and age <25;
Laravel statement:
Insert picture description here
Effect:
Insert picture description here


(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:
Insert picture description here
Effect:
Insert picture description here


(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)
Insert picture description here
Effect:
Insert picture description here


(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;
Insert picture description here
effect:
Insert picture description here


(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.

Insert picture description here
effect:
Insert picture description here


(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:
Insert picture description here
effects:
Insert picture description here
(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();

Insert picture description here
effect:
Insert picture description here


(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.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/113770792