Blogger Information
Blog 14
fans 0
comment 1
visits 12855
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel扩展数据库访问类
王珂
Original
1200 people have browsed it

laravel扩展数据库访问类

在laravel中使用数据库查询DB::table(‘admin’)->get()->toArray();会生成数组下的对象,既有数组又有对象,在使用中比较麻烦、容易出错。因此需要对数据库访问类进行扩展,在laravel7\app\Providers下生成DBServiceProvider.php,在Admin.php中就可以使用lists()方法。生成的数据为二维数组。
注:并不是所有类都可扩展,macroable的才可扩展。

DBServiceProvider.php

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Database\Query\Builder as QueryBuilder;
  5. class DBServiceProvider extends ServiceProvider
  6. {
  7. public function boot()
  8. {
  9. //
  10. QueryBuilder::macro('lists',function(){
  11. $data = $this->get()->toArray();
  12. $result = [];
  13. foreach($data as $val){
  14. $result[] = (array)$val;
  15. }
  16. return $result;
  17. });
  18. }
  19. }
  20. ```Admin.php
  21. ###
  22. <?php
  23. namespace App\Http\Controllers\admins;
  24. use App\Http\Controllers\Controller;
  25. use Illuminate\Http\Request;
  26. use Illuminate\Support\Facades\Auth;
  27. use Illuminate\Support\Facades\DB;
  28. // 管理员管理
  29. class Admin extends Controller
  30. {
  31. // 账号列表
  32. public function index(){
  33. $data['admins'] = DB::table('admin')->lists();
  34. foreach($data['admins'] as $key => $val) {
  35. $group = DB::table('admin_group')->where('gid',$val['gid'])->first();
  36. $data['admins'][$key]['group_title'] = $group->title;
  37. }
  38. /*
  39. $data['admins'] = DB::table('admin')->get()->toArray();
  40. foreach($data['admins'] as $key => $val) {
  41. $group = DB::table('admin_group')->where('gid',$val->gid)->first();
  42. $data['admins'][$key]->group_title = $group->title;
  43. }
  44. */
  45. return view('admins/admin/index',$data);
  46. }
  47. }

index.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>账号列表</title>
  5. <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
  7. <style>
  8. </style>
  9. </head>
  10. <body style="padding: 10px;">
  11. <table class="layui-table">
  12. <thead>
  13. <tr>
  14. <th>ID</th>
  15. <th>用户名</th>
  16. <th>分组</th>
  17. <th>真实姓名</th>
  18. <th>最后登录时间</th>
  19. <th>状态</th>
  20. <th>操作</th>
  21. </tr>
  22. </thead>
  23. <tbody>
  24. @foreach($admins as $admin)
  25. <tr>
  26. <td>{{$admin['id']}}</td>
  27. <td>{{$admin['username']}}</td>
  28. <td>{{$admin['group_title']}}</td>
  29. <td>{{$admin['real_name']}}</td>
  30. <td>{{$admin['lastlogin']?date('Y-m-d H:i:s',$admin['lastlogin']):''}}</td>
  31. <td>{{$admin['status']==0?'正常':'禁用'}}</td>
  32. <td>
  33. <button class="layui-btn layui-btn-xs">修改</button>
  34. <button class="layui-btn layui-btn-danger layui-btn-xs">删除</button>
  35. </td>
  36. </tr>
  37. @endforeach
  38. </tbody>
  39. </table>
  40. </body>
  41. </html>
Correction status:Uncorrected

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!