Laravel-admin

474 阅读1分钟

文档地址

laravel-admin.org/


安装

composer require encore/laravel-admin:1.*

发布资源

php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"

安装 (安装可能会报错)

php artisan admin:install

可能出现的错误

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (S
  QL: alter table `users` add unique `users_email_unique`(`email`))

解决报错

修改该文件app\Providers\AppServiceProvider.php 替换为以下内容

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //add fixed sql

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191); //add fixed sql
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

启动laravel-admin

在浏览器打开 http://localhost/admin/ ,使用用户名 admin 和密码 admin 登录


1.创建模型

php artisan make:model Goods
2.设置模型表名等信息
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    //

    // 指定表名
    protected $table = 'goods';

    // 设置不接管created_at, updated_at两个列
    public $timestamps = false;
}

3.创建模型对应的控制器
# Windows系统
php artisan admin:make GoodsController --model=App\Goods

# Mac系统
php artisan admin:make GoodsController --model=App\\Goods
4.到app/Admin/routes.php添加路由
$router->resource('goods', GoodsController::class);
5.管理平台上添加对应地址即可

错误处理

存储文件错误

Config error.
Disk [admin] not configured, please add a disk config in `config/filesystems.php`.

config/filesystems.php 中添加 admin 字段即可

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
        'admin' => [
            'driver'     => 'local',
            'root'       => public_path('upload'),
            'visibility' => 'public',
            'url' => env('APP_URL').'/upload/',
        ],
    ],

1.注意你的web解析目录

2.注意你的.env 配置文件

# .env 配置文件
APP_URL=http://localhost:8080

Laravel-admin 左侧菜单栏默认展开

打开 config/admin.php,修改 layout , 
去掉 sidebar-collapse, 留下 sidebar-mini

验证规则

# 规则大全
https://laravel.com/docs/5.5/validation#rule-integer

扩展

树形结构

官网文档 laravel-admin.org/docs/zh/1.x…

模型

<?php

namespace App\Admin\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use ModelTree, AdminBuilder;

    protected $table = 'category';

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
		//这里根据自己的字段修改
        $this->setParentColumn('parent_id');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }
 }

admin控制器

<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CategoryController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品分类管理';

    public function index(Content $content)
    {
        return Admin::content(function ($content) {
            $content->header('商品分类管理');
            $content->body(Category::tree(function ($tree) {
                $tree->branch(function ($branch) {
                    $src = config('admin.upload.host') . '/' . $branch['image'];
                    $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

                    return "{$branch['id']} - {$branch['name']} $logo";
                });
            }));
        });
    }
	//下面是自己的代码
	//.......
 }

表单选择

$form->select('parent_id', __('Parent id'))->options(Category::selectOptions())->default(1);

配色

项目地址 github.com/Hanson/rain…

php artisan vendor:publish --tag=rainbow-blue --force
rainbow-blue 蓝色
rainbow-green 绿色
rainbow-red 红色
rainbow-orange 橙色
rainbow-yellow 黄色
rainbow-olive 橄榄
rainbow-cyan 天青
rainbow-white-blue 白蓝
rainbow-origin to revert the origin skin.(This is not a skin.) 用于回滚样式(不是皮肤)

配置 config/admin.php

    |--------------------------------------------------------------------------
    | Application Skin
    |--------------------------------------------------------------------------
    |
    | This value is the skin of admin pages.
    | @see https://adminlte.io/docs/2.4/layout
    |
    | Supported:
    |    "skin-blue", "skin-blue-light", "skin-yellow", "skin-yellow-light",
    |    "skin-green", "skin-green-light", "skin-purple", "skin-purple-light",
    |    "skin-red", "skin-red-light", "skin-black", "skin-black-light".
    |
    */
    'skin' => 'skin-green',