批改状态:合格
老师批语:其实想想laravel的操作是有套路的, 按他的步骤去做就行, 挺优雅的
whereBetween()在范围之间查询控制器:home.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;Class Home extends Controller{//whereBetwen在范围内查询public function between(){$res = DB::table('staff')->select('id','name','age')->whereBetween('age',[20,25])->get();echo '<pre>';print_r($res);}}
路由:web.php
<?phpuse Illuminate\Support\Facades\Route;Route::get('/home/between','Home@between');

whereIn()在指定数组中查询控制器:home.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;Class Home extends Controller{//whereInpublic function in(){$res = DB::table('staff')->whereIn('id',[1002,1050])->get();echo '<pre>';print_r($res);}}
路由:web.php
<?phpuse Illuminate\Support\Facades\Route;Route::get('/home/in','Home@in');

orWhere() 或查询控制器:home.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;Class Home extends Controller{//orWherepublic function whereor(){$res = DB::table('staff')->where('id',1002)->Orwhere('id',1007)->get();echo '<pre>';print_r($res->toArray());}}
路由:web.php
<?phpuse Illuminate\Support\Facades\Route;Route::get('/home/whereor','Home@whereor');

join() 多表查询控制器:home.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;Class Home extends Controller{//join查询public function join(){$res = DB::table('staff')->join('user','staff.name','=','user.name')->get();echo '<pre>';print_r($res);}}
路由:web.php
<?phpuse Illuminate\Support\Facades\Route;Route::get('/home/join','Home@join');

Mycheck.php
<?phpnamespace App\Http\Middleware;use Illuminate\Http\Request;use Closure;/*中间件固定格式*/class Mycheck{public function handle(Request $request,Closure $next){$a = 3;$b = 5;if($a+$b === 8){return response('a+b='.($a+$b),200);}return $next($request);}}
Kernel.php
<?phpnamespace App\Http;use Illuminate\Foundation\Http\Kernel as HttpKernel;class Kernel extends HttpKernel{protected $routeMiddleware = [//注册中间件'mycheck' => \App\Http\Middleware\Mycheck::class,];}
web.php
<?phpuse Illuminate\Support\Facades\Route;Route::get('/home/join','Home@join')->middleware('mycheck');

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号