博客列表 >Laravel 模型 中间件 示例

Laravel 模型 中间件 示例

海阔天空
海阔天空原创
2020年06月07日 11:11:001066浏览

Laravel 模型 中间件 示例

Laravel 模型示例

  • 利用 artisan 命令行创建Article模型
    php artisan make:model Article
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Article extends Model
  5. {
  6. //重新定向数据库表文件
  7. protected $table='article';
  8. }

Laravel 模型方法

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Http\Request;
  5. use App\Article;
  6. class Home extends Controller
  7. {
  8. //model 模型
  9. public function mymodels(Article $article){
  10. echo '<br> mymodel.blade执行了...';
  11. //将返回的模型类转为数组
  12. $res=$article->get()->toArray();
  13. // echo '<pre>';
  14. // print_r($res);
  15. return view ('mymodel',['items'=>$res]);
  16. }
  17. }

Laravel blade模板文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/layui/css/layui.css" />
  7. <title>模型、中间件示例</title>
  8. </head>
  9. <body>
  10. <table class="layui-table">
  11. <thead>
  12. <tr>
  13. <td>ID</td>
  14. <td>CATE_ID</td>
  15. <td>标题</td>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. @foreach ($items as $val)
  20. <tr>
  21. <td>{{$val['id']}}</td>
  22. <td>{{$val['cate_id']}}</td>
  23. <td>{{$val['title']}}</td>
  24. </tr>
  25. @endforeach
  26. </tbody>
  27. </table>
  28. </body>
  29. </html>

Laravel 中间件创建

命令行 php artisan make:middleware Mymiddle

  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. class Mymiddle
  5. {
  6. /**
  7. * Handle an incoming request.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @param \Closure $next
  11. * @return mixed
  12. */
  13. public function handle($request, Closure $next)
  14. {
  15. echo "这是一个中间件...";
  16. return $next($request);
  17. }
  18. }

注册中间件

  1. protected $routeMiddleware = [
  2. 'auth' => \App\Http\Middleware\Authenticate::class,
  3. 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
  4. 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
  5. 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
  6. 'can' => \Illuminate\Auth\Middleware\Authorize::class,
  7. 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
  8. 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
  9. 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
  10. 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
  11. 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
  12. 'mymiddle' => \App\Http\Middleware\Mymiddle::class,
  13. ];

触发中间件

  1. Route::get('/dbmodels','home@mymodels')->middleware('mymiddle');

效果图

总结:
1、Laravel 模型中需重映射数据库表文件。
2、中间件需 创建-注册-触发。
3、命令行 创建模型、中间件等,需要把当前目录定为artisan所在目录。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议