关于Laravel-admin的基础用法总结和自定义model详解

吾爱主题 阅读:166 2021-08-26 14:41:00 评论:0

总结laravel-admin展示用到的基本方法

基础用法

自定义model

?
1 2 3 4 5 6 7 当列表数据获取有特定条件或自己写ORM方法时可以用到,支持排序 $grid ->model()->select( 'id' , 'name' )->where( 'status' ,1)->groupBy( 'project_id' ); $grid ->model()->select( 'id' , 'name' )->where( 'status' ,1)->groupBy( 'project_id' ); //结合having 查出名字重复的 $grid ->model()->select( 'name_en' )->groupBy( 'name_en' )->havingRaw( 'count(name_en)>1' ) //嵌套子查询 查出名字重复的全部信息 $grid ->model()->select( 'id' , 'name_en' )->whereIn( 'name_en' ,Project::select( 'name_en' )->groupBy( 'name_en' )->havingRaw( 'count(name_en)>1' ))->orderBy( 'name_en' );

模型数据获取

第一列显示id字段,并将这一列设置为可排序列

?
1 $grid ->id( 'ID' )->sortable();

获取单列数据的方法

?
1 $grid ->name_cn( '名称' );

注:name_cn为与数据库对应的字段名

?
1 $grid ->column( 'name_cn' , '名称' );

判断type来显示不同的状态

?
1 2 3 $grid ->column( 'type' , '类型?' )->display( function ( $type ) {   return $type == 1 ? '111' : '222' ; });

where条件

?
1 $grid ->model()->where( 'type' , 0);

三个时间的显示

?
1 2 3 4 // 下面为三个时间字段的列显示 $grid ->release_at(); $grid ->created_at(); $grid ->updated_at();

筛选框控制方法

基本方法

?
1 2 3 4 5 6 7 8 9 10 11 12 13 //filter($callback)方法用来设置表格的简单搜索框 $grid ->filter( function ( $filter ) {   //1.时间段筛选 设置created_at字段的范围查询   $filter ->between( 'created_at' , '筛选时间' )->datetime();   //2.字段模糊查询 like = '% %'   $filter ->like( 'name' , '姓名' );   //3.字段equal 筛选   $filter ->equal( 'status' , '状态' )->select([0 => '下线' , 1 => '上线' ]);   //4.去掉默认的ID搜索   $filter ->disableIdFilter();   //5.   });

按钮控制

禁用导出

?
1 $grid ->disableExport();

禁用新增

?
1 $grid ->disableCreateButton();

禁用行选择checkbox

?
1 $grid ->disableRowSelector();

去掉重置 [from]

?
1 $form ->disableReset();

关闭默认行操作

?
1 2 3 4 5 6 7 8 $grid ->actions( function ( $actions ) {    //关闭删除   $actions ->disableDelete();   //关闭编辑   $actions ->disableEdit();   //自定义操作按钮   $actions ->append( '<button type="button" class="btn btn-danger noShow" data-id="' . $actions ->getKey() . '" >隐藏</button>' ); });

关闭批量删除

?
1 2 3 4 5 6 $grid ->tools( function ( $tools ) {   //关闭批量删除   $tools ->batch( function ( $batch ) {    $batch ->disableDelete();   }); });

FORM表单提交

禁用重置按钮

?
1 $form ->disableReset();

文本输入框

?
1 2 //默认展示$data['name']的值,新接收的值存储user表name字段 $form ->text( 'user.name' , '名称' )-> default ( $data [ 'name' ]);

上传图片/文件

?
1 2 3 4 5 6 7 $form ->image( 'user.logo' , 'logo' )      #随机文件名      ->uniqueName()      #验证文件格式( 'mimes:doc,docx,xlsx' );      ->rules( 'mimes:png' )      #输入框下边的help提示语      ->help( $str );

表单提交url

?
1 2 3 4 5 $form ->url( 'user.website' , '官网' )      #默认填充url 传参      -> default ( $url )      #提示的url      ->help( 'eg: http://www.aware.bi' );

表单提交下拉框

?
1 2 3 4 #下拉框展示 $message 提示语 $form ->multipleSelect( 'project.tags1' , $message )      #下拉框数据      ->options( $tags [ 'children' ]);

select下来

?
1 2 $types = array ( '0' => '教育' , '1' => '医疗' ); $form ->select( 'type' , '类型' )->options( $types );

laravel SQL取值

?
1 $users = User::all()->pluck( 'name' , 'id' )->toArray();

表单输入HTML editor编辑器

?
1 $form ->editor( 'detail' , '详细介绍' );

单选按钮 样式转换

?
1 2 3 4 5 $states = [   'on' => [ 'value' => 1, 'text' => '上线' , 'color' => 'success' ],   'off' => [ 'value' => 0, 'text' => '下线' , 'color' => 'danger' ], ]; $form -> switch ( 'status' , '上/下线' )->states( $states );

隐藏域

?
1 $form ->hidden( 'is_in' );

保存数据的回调

?
1 2 3 4 5 6 7 8 9 10 $form ->saving( function (Form $form ) {   #指定值为固定1   $form ->is_in = 1;   #验证值是够有重复   if ( $from ->nick_name !== $form ->model()->email && User::where( 'email' , $form ->email)->value( 'id' )){   #错误信息提示   $error = new MessageBag([ 'title' => '提示' , 'message' => '邮箱已存在!' ]);    return back()->withInput()->with(compact( 'error' ));   } });

自定义按钮操作

我们先自定义了一个隐藏按钮

?
1 2 3 4 5 6 $grid ->actions( function ( $actions ) {   //自定义操作按钮   $actions ->append('<button type= "button" class = "btn btn-danger noShow"   data-id= "' . $actions->getKey() . '" >隐藏</button>');   //当前数据的ID });

在controller写JS文件把执行JS渲染到模板

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46     $js = <<<EOD     <script>     //隐藏的点击事件 $(document).on( 'click' , '.noShow' , function () {   //获取的ID   var id = $(this).data( 'id' );    console.log(id);   swal({    title: "确认隐藏?" ,    type: "warning" ,    showCancelButton: true,    confirmButtonColor: "#DD6B55" ,    confirmButtonText: "确认" ,    closeOnConfirm: false,    cancelButtonText: "取消"   },   function (){    $.ajax({     method: 'post' ,     url: '/admin/articles/hidden' ,     data: {      //文章ID      id:id,      //post请求token      _token:LA.token,     },     success: function (data) {      $.pjax.reload( '#pjax-container' );      if (typeof data === 'object' ) {       if (data.status == 1) {        swal(data.msg, '' , 'success' );       } else {        swal(data.msg, '' , 'error' );       }      }     }    })   }   ) });     </script> EOD;     //传递到页面     $content ->body( $js );     //执行你的model     $content ->body();

以上这篇关于Laravel-admin的基础用法总结和自定义model详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/guoyanga1/article/details/80774570

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容