按顺序执行以下命令
composer require encore/laravel-admin:1.*
php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
php artisan admin:install
- 在数据库中建立数据表
- 在命令行生成数据表的模型
- 使用命令生成表对应的控制器(控制器会自动生成对应的部分代码)
php artisan admin:make BrandController --model=App\Brand
$router->resource('brands', BrandController::class);
- 然后登录账号密码(admin:admin)
- 上传图片添加配置(在config/filesystems.php文件中修改)
'admin' => [
'driver' => 'local',
'root' => public_path('uploads'),
'visibility' => 'public',
'url' => env('APP_URL').'/uploads',
],
//表单上传图片
$form->image('logo', __('Logo'));
//展示图片
$grid->column('logo', '商标')->image('http://www.day8.com/uploads');
完整控制器代码
<?php
namespace App\Admin\Controllers;
use App\Brand;
use App\Catogor;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class BrandController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = 'Brand';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new Brand());
$grid->paginate(4);
$grid->column('id', __('Id'));
$grid->column('sort', __('Sort'));
$grid->column('logo', '商标')->image('http://www.day8.com/uploads');
$grid->column('title', __('Title'));
$grid->column('catogory', '品牌')->display(function ($catogory) {
return Catogor::find($catogory)->cato_name;
});
// $grid->column('catogory', __('Catogory'));
$grid->column('created_at', __('Created at'));
$grid->column('updated_at', __('Updated at'));
$grid->perPages([4, 5, 10, 15]);
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(Brand::findOrFail($id));
$show->field('id', __('Id'));
$show->field('sort', __('Sort'));
$show->field('logo', __('Logo'));
$show->field('title', __('Title'));
$show->field('catogory', __('Catogory'));
$show->field('created_at', __('Created at'));
$show->field('updated_at', __('Updated at'));
return $show;
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new Brand());
$form->number('sort', __('Sort'));
$form->image('logo', __('Logo'));
$form->text('title', __('Title'));
$directors = $this->cato();
$form->select('catogory', '分类')->options($directors);
return $form;
}
/*
获取品牌分类名称
*/
public function cato()
{
$arr = Catogor::all()->toArray();
$id = array_column($arr, 'id');
$cato_name = array_column($arr, 'cato_name');
$arr = array_combine($id, $cato_name);
return $arr;
}
}
|