侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

laravel-admin中模型及控制器的创建使用步骤

Laravel  /  管理员 发布于 2年前   3409

这篇也是接上上一篇的,laravel-admin扩展包装好了就使用一下它

环境:centos7+laravel7+laravel-admin1.8

mysql创建个表:test 添加几个字段id,name,添加时间,编辑时间

1.创建model

[root@www laraveltest]# php artisan make:model Models/Test
Model created successfully.

2.创建控制器

[root@www laraveltest]# php artisan admin:make TestController --model=App\\Models\\Test
App\Admin\Controllers\TestController created successfully.

Add the following route to app/Admin/routes.php:

   $router->resource('tests', TestController::class);

[root@www laraveltest]#

3.在菜单栏Admin\Menu里面添加菜单 测试目录 url设置tests

4.设置路由 (app\admin\routes.php)

//$router->resource('tests', TestController::class);

$router->resources([
       'tests' => TestController::class,
]);

5.test模型

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
   protected $table = 'test';

   public static function grid($callback)
   {
       return new Grid(new static, $callback);
   }
   public static function form($callback)
   {
       return new Form(new static, $callback);
   }
}

6.test控制器

<?php
namespace App\Admin\Controllers;

use App\Models\Test;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class TestController extends AdminController
{
   /**
    * Title for current resource.
    * @var string
    */
   protected $title = 'Test';

   /**
    * Make a grid builder.
    * @return Grid
    */
   protected function grid()
   {
       $grid = new Grid(new Test());

       $grid->column('id', 'ID')->sortable();
       $grid->column('name');
       $grid->column('created_at')->date('Y-m-d');
       $grid->column('updated_at')->date('Y-m-d');

       return $grid;
   }

   /**
    * Make a show builder.
    * @param mixed $id
    * @return Show
    */
   protected function detail($id)
   {
       $show = new Show(Test::findOrFail($id));
       return $show;
   }

   /**
    * Make a form builder.
    * @return Form
    */
   protected function form()
   {
       $form = new Form(new Test());
       return $form;
   }
}

ok了

1.png


  • 上一条:
    laravel7+安装laravel-admin操作流畅步骤及mysql5.5版本报错处理
    下一条:
    web开发-前后端分离原理/思想浅析
  • 昵称:

    邮箱:

    1条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 10.4版本发布(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在laravel项目中实现密码强度验证功能推荐扩展包:password-strength(0个评论)
    • Laravel Response Classes 响应类使用优化浅析(0个评论)
    • 在Laravel中使用FilePond上传文件及测试用例(0个评论)
    • 近期文章
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 在go语言中使用GoPDF包把html生成PDF文件示例(0个评论)
    • 在go语言中创建和解析(读取)符号链接示例(0个评论)
    • ubuntu 22.04系统中报错:Python 3.6 is no longer supported by the Python core team...解决方式(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客