Blogger Information
Blog 55
fans 3
comment 0
visits 54983
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库访问类链式查询方法
王佳祥
Original
1350 people have browsed it

数据库访问类链式查询方法

一、查询构造器

1.first():查询一条记录

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. public function chain(){
  8. //链式调用
  9. $res2 = DB::table('staff')->where('id',1002)->first(['id','name']);
  10. echo '<pre>';
  11. print_r($res2);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/chain','Home@chain');


2.get():查询多条记录

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //查询多条记录
  8. public function lists(){
  9. $res = DB::table('staff')->where('sex',2)->get();
  10. $data = $res->toArray();
  11. echo '<pre>';
  12. print_r($res);
  13. echo '<hr>';
  14. print_r($data);
  15. }
  16. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/list','Home@lists');


3.value():查询某一列的当前值

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //查询某一列的当前值
  8. Public function val(){
  9. $res = DB::table('staff')->where('id',1002)->first(['name']);
  10. $res2 = DB::table('staff')->where('id',1002)->value('name');
  11. echo '<pre>';
  12. print_r($res);
  13. print_r($res2);
  14. }
  15. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/val','Home@val');
  • first() 返回对象的键和值

  • value()返回值



4.piuck():返回指定字段组成的集合

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //pluck():返回指定字段组成的集合
  8. public function pluck(){
  9. $res = DB::table('staff')->pluck('name');
  10. $res2 = DB::table('staff')->get('name');
  11. $res3 = DB::table('staff')->pluck('name','id');
  12. echo '<pre>';
  13. print_r($res);
  14. print_r($res2);
  15. print_r($res3);
  16. }
  17. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/pluck','Home@pluck');
  • pluck(value) : 返回指定key的值组成的集合

  • pluck(value, key) : 返回指定value,key的值组成的集合



二、链式操作的增删改查

1.新增操作:insert()

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //新增记录
  8. public function insert2(){
  9. $data = [
  10. ["name"=>"诸葛亮","sex"=>1,"age"=>40,"hiredate"=>"2020-09-17"],
  11. ["name"=>"貂蝉","sex"=>0,"age"=>28,"hiredate"=>"2020-09-17"],
  12. ["name"=>"吕布","sex"=>1,"age"=>33,"hiredate"=>"2020-09-17"],
  13. ["name"=>"西施","sex"=>0,"age"=>30,"hiredate"=>"2020-09-17"],
  14. ];
  15. $res = DB::table('staff')->insert($data);
  16. var_dump($res);
  17. }
  18. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/insert2','Home@insert2');




1.更新操作:update()

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //更新记录
  8. public function update2(){
  9. $res = DB::table('staff')->where('sex',1)->update(['sex'=>2]);
  10. var_dump($res);
  11. }
  12. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/update2','Home@update2');




3.删除操作:delete()

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //删除记录
  8. public function delete2(){
  9. $res = DB::table('staff')->where('id',1048)->delete();
  10. var_dump($res);
  11. }
  12. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/delete2','Home@delete2');


三、聚合查询

1.max():最大值

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //聚合查询
  8. public function juhe(){
  9. //最大值
  10. $res = DB::table('staff')->max('salary');
  11. var_dump($res);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/juhe','Home@juhe');


2.min():最小值

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //聚合查询
  8. public function juhe(){
  9. //最小值
  10. $res = DB::table('staff')->min('salary');
  11. var_dump($res);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/juhe','Home@juhe');


3.avg():平均值(所有记录的平均值)

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //聚合查询
  8. public function juhe(){
  9. //平均值
  10. $res = DB::table('staff')->avg('salary');
  11. var_dump($res);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/juhe','Home@juhe');


4.sum():求和

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //聚合查询
  8. public function juhe(){
  9. //求和
  10. $res = DB::table('staff')->sum('salary');
  11. var_dump($res);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/juhe','Home@juhe');

5.count():统计记录条数

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. //聚合查询
  8. public function juhe(){
  9. //count统计记录
  10. $res = DB::table('staff')->count('salary');
  11. var_dump($res);
  12. }
  13. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/juhe','Home@juhe');


6.as:临时别名

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. public function select(){
  8. $res = DB::table('staff')->select('id','name as username','age as userage')->get();
  9. echo '<pre>';
  10. print_r($res);
  11. }
  12. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/select','Home@select');


7.dump()打印输出结果,执行完继续执行下面的代码(只做调试用)

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. public function select(){
  8. $res = DB::table('staff')->select('id','name as username','age as userage')->get();
  9. dump($res);
  10. echo '<h1>继续执行</h1>';
  11. }
  12. }


8.dd()执行完直接终止,不再执行下面的代码(只做调试用)

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. public function select(){
  8. $res = DB::table('staff')->select('id','name as username','age as userage')->get();
  9. dd($res);
  10. echo '<h1>继续执行</h1>';
  11. }
  12. }


9.where():多条件查询

控制器:Home.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. Class Home extends Controller{
  6. //链式操作
  7. public function where(){
  8. $res = DB::table('staff')->where('age','<',30)->where('salary','<',10000)->get();
  9. echo '<pre>';
  10. print_r($res);
  11. }
  12. }

路由:web.php

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. Route::get('/home/where','Home@where');


Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:坚持到现在不容易,继续
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post