Blogger Information
Blog 63
fans 8
comment 8
visits 50030
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP大牛成长之路:构造器的 where 派生查询及中间件
周Sir-BLOG
Original
1009 people have browsed it

1、构造器的 where 派生查询

1.1、 实现区间查询 whereBetween()

  1. //查询年龄为18~19岁的纪录(对象)
  2. $res = DB::table('users')->whereBetween('age',[18,19])->get();
  3. //将查询结果转为数组
  4. $res = DB::table('users')->whereBetween('age',[18,19])->get()->toArray();
  5. //将结果输出sql语句
  6. $res = DB::table('users')->whereBetween('age',[18,19])->toSql();

1.2、 实现数组匹配的记录 whereIn()

  1. //查询id为2010,2011的记录
  2. $res = DB::table('users')->whereIn('id', [2010,2011])->get();

1.3、 or 条件查询 orWhere()

  1. $res = DB::table('users')
  2. ->where('age', '<', 20)
  3. ->orWhere('gender', '<', '女')
  4. ->get();

1.4、 内联接的多表查询 join()

  1. // select * from admin,admin_group where admin.gid=admin_group.gid
  2. $res = DB::table('admin')
  3. ->join('admin_group', 'admin.gid', '=', 'admin_group.gid')
  4. ->get();

2、中间件(就是在方法执行前中间执行的方法)

2.1、第1步:创建中间件

  • App\Http\Middleware目录下新建:MyCheck.php
  1. namespace App\Http\Middleware;
  2. use Illuminate\Http\Request;
  3. use Closure;
  4. class MyCheck
  5. {
  6. public function handle(Request $request, Closure $next)
  7. {
  8. $flag=false;
  9. if (!$flag) {
  10. return response('数据错误');
  11. }
  12. return $next($request);
  13. }
  14. }

2.2、第2步:注册中间件

  • App\Http 目录下的 Kernel.php 文件的路由数组:$routeMiddleware中注册:
  1. //注册中间件
  2. 'mycheck' => \App\Http\Middleware\MyCheck::class,

2.3、第3步:指定路由中间件

  • routes 目录下的 web.php 文件的路由方法中指定路由中间件
  1. Route::get('users/dbtest', 'Users@dgbtest')->middleware('mycheck');
  • 访问dgbtest方法前就会先执行中间件;

总结:

1、对构造器的 where 派生查询的部分函数进行了了解;
2、内联接的多表查询方法理解还不够深入;
3、中间件的基本步骤进行了了解。

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