在使用laravel-admin开发的过程中,根据官方开发文档Laravel admin | laravel-admin基本都能实现想要的效果,这里补充3个文档上没有描述的细节
在laravel-admin中可以使用php artisan admin:make UserController --model=App\User
命令创建后台控制器,后面的--model
是绑定的模型,它会根据model对应表的字段,默认构建出所需的grid,form和show三个页面的代码,但是由于laravel8模型额目录不再是App
,而是App\Models
,所以命令后面的模型路径也要修改为php artisan admin:make UserController --model=App\Models\User
,否则会报错The "--model" option does not exist.
方法一:使用默认时间格式
如果想使用默认的2020-03-04 16:11:00
格式,在你的模型里面引入Encore\Admin\Traits\DefaultDatetimeFormat
,然后use DefaultDatetimeFormat;
use Encore\Admin\Traits\DefaultDatetimeFormat;
class User extends Model
{
use DefaultDatetimeFormat;
}
方法二:自定义时间格式
Traits
,然后新建文件SerializeDate.php
,文件内容如下
namespace App\Traits;
use DateTimeInterface;
trait SerializeDate
{
/**
* 为 array / JSON 序列化准备日期格式
*
* @param \DateTimeInterface $date
* @return string
*/
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
2.在model中use SerializeDate
即可
namespace App\Models;
use app\Traits\SerializeDate;
class Users extends Model
{
use HasFactory,SerializeDate;
//
}
laravel-admin自带的语言本地化只有部分后台按钮和菜单的翻译,表格字段和编辑页字段的翻译是没有的,如果需要添加,只需要在\resources\lang
目录下新建一个zh-CN.json
文件,然后用__()
方法使用即可,这样的话,其它控制器也能复用这里的翻译
zh-CN.json
文件内容格式如下:
{
"Id": "ID",
"Created at":"创建时间",
"Updated at":"更新时间",
"Name":"用户名",
"Password":"密码",
"Email":"邮箱",
"Email verified at":"邮箱验证时间"
}
使用:
namespace App\Admin\Controllers;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class UserController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = '用户';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new User());
$grid->column('id', __('Id'));
$grid->column('name', __('Name'));
$grid->column('email', __('Email'));
$grid->column('email_verified_at', __('Email verified at'));
$grid->column('created_at', __('Created at'));
$grid->column('updated_at', __('Updated at'));
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(User::findOrFail($id));
$show->field('id', __('Id'));
$show->field('name', __('Name'));
$show->field('email', __('Email'));
$show->field('email_verified_at', __('Email verified at'));
$show->field('password', __('Password'));
$show->field('remember_token', __('Remember token'));
$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 User());
$form->text('name', __('Name'));
$form->email('email', __('Email'));
$form->datetime('email_verified_at', __('Email verified at'))->default(date('Y-m-d H:i:s'));
$form->password('password', __('Password'));
$form->text('remember_token', __('Remember token'));
return $form;
}
}
[ichynul/iframe-tabs: laravel-admin]使用tab控制多个iframe打开多页面(Use tabs to control multiple iframes to open multiple pages) (github.com)
效果: