Blogger Information
Blog 37
fans 0
comment 0
visits 21173
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
综合实战第九课:后台系统开发2-PHP培训九期线上班
渡劫小能手
Original
507 people have browsed it

扩展数据库访问类

为了让查询数据库出来的数据直接以数组的方式返回

条件

类里面use了Macroable,就可以扩展

定义扩展

在app\providers目录下建立文件

  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. QueryBuilder::macro('item',function(){
  10. $res = $this->first();
  11. return $res == null?false:(array)$res;
  12. });
  13. QueryBuilder:: macro('lists',function(){
  14. $res = $this->get()->all();
  15. foreach ($res as $key => $value){
  16. $res[$key] = (array)$value;
  17. }
  18. return $res;
  19. });
  20. }
  21. }

注册扩展

在config目录下的app文件内的providers下

  1. //注册DBServiceProvider
  2. App\Providers\DBServiceProvider::class,

blade模板的转义

{{}} 支持转义 一段html代码只是被当成普通的字符串输出
{!! !!} 不支持转移 一段html代码可以被正常的解析

分组表中数字对应汉字


使用2次查询,相互调用

  1. <td>{{isset($groups[$admin['group_id']])? $groups[$admin['group_id']]['title']:''}}</td>

改造数组下标

使数组下标和gid对应上

  1. $groups = DB::table('xpcms_admin_group')->lists();
  2. $result = [];
  3. foreach ($groups as $key => $value) {
  4. $result[$value['gid']] = $value;
  5. }

添加管理员

在add.blade.php页面使用ajax的post方法提交数据到控制器中的save方法
在表中查重
在控制器admin.php中使用save方法接收数据,插入数据库,返回json结果

  1. <script type="text/javascript">
  2. function admin_save(){
  3. var data = new Object();
  4. data.username = $.trim($('#username').val());
  5. data.group_id = $.trim($('#group_id').val());
  6. data.pwd = $.trim($('#pwd').val());
  7. data.real_name = $.trim($('#real_name').val());
  8. data.status = $('#status').is(':checked')?1:0;
  9. data._token = $('input[name="_token"]').val();
  10. if(username==''){
  11. return layer.alert('用户名不能为空',{icon:2});
  12. }
  13. $.post('/admins/admin/save',data,function(res){
  14. if(res.code>0){
  15. return layer.alert(res.msg,{icon:2});
  16. }
  17. layer.msg(res.msg,{icon:1});
  18. setTimeout(function(){parent.window.location.reload();},1000);
  19. },'json');
  20. }
  21. </script>
  1. public function save(Request $req){
  2. $data['username'] = trim($req->username);
  3. $data['password'] = trim($req->pwd);
  4. $data['group_id'] = (int)$req->group_id;
  5. $data['real_name'] = trim($req->real_name);
  6. $data['status'] = (int)$req->status;
  7. $data['add_time'] = time();
  8. //查重
  9. $admin = DB::table('xpcms_admin')->where('username',$data['username'])->item();
  10. if ($admin) {
  11. return json_encode(array('code'=>1,'msg'=>'该用户已经存在'))
  12. }
  13. DB::table('xpcms_admin')->insert($data);
  14. return json_encode(array('code'=>0,'msg'=>'保存成功'));
  15. }
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