laravel-admin CURD

laravel-admin 实现文章列表、添加、编辑、删除、分页、检索 laravel-admin 基础增删改查

效果图

laravel-admin curd
laravel-admin 入库

涉及文件

|--app
|--|--Admin
|--|--|--Controllers
|--|--|--|TestController.php #手动创建
|--|--|--routers.php
|--|--Models #手动创建
|--|--|--Movie.php #手动创建

TestController.php

<?php
namespace App\Admin\Controllers;
use App\Models\Movie;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class TestController extends AdminController
{
    protected $title = '测试';

    protected function grid()
    {
        $grid = new Grid(new Movie());
        $grid->id('ID')->sortable();
        $grid->title('标题')->editable();
        $grid->director()->editable();
        $grid->describe()->editable();
        $grid->created_at();
        $grid->updated_at();
        return $grid;
    }
    protected function form()
    {
        $form = new Form(new Movie());
        $form->display('id', 'ID');
        $form->text('title', '标题')->rules('required');
        $form->text('director')->rules('required');
        $form->text('describe')->rules('required');
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');
        return $form;
    }
}

Movie.php

<?php
namespace App\Models;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class Movie extends Model implements Sortable
{
    use SortableTrait;

    protected $table = 'movies';

    public $sortable = [
        'order_column_name' => 'rate',
        'sort_when_creating' => true,
    ];

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

routers.php

<?php

use Illuminate\Routing\Router;

Admin::routes();

Route::group([
    'prefix'        => config('admin.route.prefix'),
    'namespace'     => config('admin.route.namespace'),
    'middleware'    => config('admin.route.middleware'),
], function (Router $router) {

    $router->get('/', 'HomeController@index')->name('admin.home');
    // CURD 路由
    $router->resources([
        'test'  => TestController::class,
    ]);
});

Tip: 查看composer.json如果下面包没有则需要安装

composer require "spatie/eloquent-sortable:^3.4"

composer require "spatie/laravel-backup:^5.0"

数据库表

CREATE TABLE `movies` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `director` int(10) unsigned NOT NULL,
  `describe` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `rate` tinyint unsigned NOT NULL,
  `released` enum('0', '1'),
  `release_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Tip: SQL执行报错 ERROR 1067 (42000): Invalid default value for ‘release_at’

处理 === 去掉 sql_mode 中的 values: NO_ZERO_IN_DATE,NO_ZERO_DATE 即可

进入mysql 执行命令 show variables like 'sql_mode';

然后复制值 set session
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
再重新设置值(Tip: 不要直接复制执行)

菜单添加

laravel-admin 菜单

相关资料

参考-ArticleController-demo
ERROR 1067-mysql报错

发表评论