1,安装
composer require encore/laravel-admin:1.* php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider" php artisan admin:install
2,启动服务后,在浏览器打开 http://localhost/admin/
,使用用户名 admin
和密码 admin
登录.
3,如何登录成功,修改路由进行配置。
在路由配置文件app/Admin/routes.php
里添加一行:$router->resource('/water', WaterController::class);
use Illuminate\Routing\Router; Admin::routes(); Route::group([ 'prefix' => config('admin.route.prefix'), 'namespace' => config('admin.route.namespace'), 'middleware' => config('admin.route.middleware'), 'as' => config('admin.route.prefix') . '.', ], function (Router $router) { $router->get('/', 'HomeController@index')->name('home'); $router->resource('/water', WaterController::class); });:
4,创建model文件。在文件夹app\Models下面创建模型文件Water.php。创建时应该在数据库中创建相应的数据表
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Water extends Model { protected $table = 'water'; public $timestamps = false;//会禁止默认的create_at 和 `updated_at 字段使用 }
5,创建控制器。在文件夹app\Admin\Controllers 创建文件WaterController.php
<?php namespace App\Admin\Controllers; use Encore\Admin\Controllers\AdminController; use Encore\Admin\Form; use Encore\Admin\Grid; use Encore\Admin\Show; use Encore\Admin\Facades\Admin; use App\Models\Water; class WaterController extends AdminController { /** * Title for current resource. * * @var string */ protected $title = '水情信息'; /** * Make a grid builder. *列表页 * @return Grid */ protected function grid() { $grid = new Grid(new Water); // 搜索 $grid->filter(function($filter){ $filter->column(1/2, function ($filter) { $filter->like('title'); $filter->between('rate'); }); $filter->column(1/2, function ($filter) { $filter->equal('created_at')->datetime(); $filter->between('updated_at')->datetime(); $filter->equal('released')->radio([ 1 => 'YES', 0 => 'NO', ]); }); /* $filter->disableIdFilter();//去掉默认的id过滤器 $filter->like('name1', '名称1'); $filter->between('datetime', '发布日期')->date(); $filter->in('time1', '时间')->select([ 1 => '华为', 2 => '小米', 3 => 'OPPO', 4 => 'vivo', ]); $filter->in('upfile', '状态')->checkbox([ 0 => '未知', 1 => '已下单', 2 => '已付款', 3 => '已取消', ]); $filter->group('name2', function ($group) { $group->gt('大于'); $group->lt('小于'); $group->nlt('不小于'); $group->ngt('不大于'); $group->equal('等于'); }); $filter->equal('datetime')->datetime(); $filter->equal('time2')->radio([ 1 => 'YES', 0 => 'NO', ]); */ }); //$grid->model()->where('id', '>', 2900); $grid->model()->orderBy('id', 'desc'); $grid->id('id')->sortable(); //$grid->title('标题'); $grid->name1('名称1'); $grid->name2('名称12'); $grid->time1('时间'); $grid->uppic('图片')->image();; $grid->datetime('修改时间'); $grid->paginate(30); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(Water::findOrFail($id)); $show->panel() ->style('danger') ->title('基本信息'); $show->field('id', __('ID')); $show->field('name1', '名称1'); $show->field('name2', '名称2'); $show->field('uppic', '图片')->image() ; $show->time1()->label(); $show->divider(); $show->field('datetime', '修改时间'); return $show; } /** * Make a form builder. * * @return Form */ protected function form() { //admin_toastr('Message...', 'success'); //admin_toastr('Message...', 'success', ['timeOut' => 5000]); $form = new Form(new Water); $form->display('id', __('ID')); //$form->text('titel', '标题'); $form->text('name1', '名称1')->placeholder('请输入名称'); $form->text('name2', '名称2'); $form->text('time1', '时间'); $form->datetime('dateTime', '发布时间'); $form->number('name3', '打分'); $form->image('uppic','图片上传')->thumbnail('small', $width = 300, $height = 300);; $form->file('upfile','文件上传'); // 添加开关操作 //$form->switch('released', '发布'); //$form->html(view('admin.public') ); return $form; } }
6,集成ckeditor编辑器
composer require laravel-admin-ext/ckeditor
php artisan vendor:publish --tag=laravel-admin-ckeditor
修改config/admin.php
'extensions' => [ 'ckeditor' => [ //Set to false if you want to disable this extension 'enable' => true, // Editor configuration 'config' => [ ] ] ]
修改 config.js 添加如下代码
config.filebrowserImageUploadUrl='/admin/upload'; config.filebrowserUploadMethod ='form'
修改ckeditor/plugins/image/dialogs/image.js
中搜索Upload 把id:"Upload",hidden:!0, 更改id:"Upload",hidden:false,
修改routes/web.php增加
Route::post('/admin/upload','CkeditorUploadController@uploadImage');
创建文件上传控制器
php artisan make:controller ckeditorUploadcontroller
文件内容是
namespace App\Admin\Controllers; use Encore\Admin\Controllers\AdminController; use Encore\Admin\Form; use Encore\Admin\Grid; use Encore\Admin\Show; use Encore\Admin\Facades\Admin; use Illuminate\Support\Facades\Storage; use Encore\Admin\Layout\Content; use Illuminate\Http\Request; class ckeditorUploadcontroller extends AdminController { public function uploadImage(Request $request){ $image=request()->file('upload'); $path=$image->store('images'); $url=Storage::disk('admin')->url($path); $callback=$request->input("CKEditorFuncNum"); $CKEditor=$request->input("CKEditor"); return "<script>window.parent.CKEDITOR.tools.callFunction(1,'{$url}','')</script>"; } }
编辑器调用的页面直接使用如下方式调用编辑器
$form = new Form(new Water); $form->ckeditor('content');