christhompsontldr / laraman
Laravel CRUD 管理器。
Requires
- laravelcollective/html: ^6.0
README
Laraman 是一个基于 Laravel 的管理面板。
Laraman 提供了快速的用户界面,用于审查和管理存储在数据库中的数据。
Laraman 在索引路由、搜索、过滤和分页方面表现非常出色。它将创建、更新和删除留给应用程序。
安装
Composer
使用 composer 安装此包
composer require christhompsontldr/laraman
服务提供者
更新 composer 后,将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组
Laravel 5.x
Christhompsontldr\Laraman\ServiceProvider::class,
配置
将包中的 config/laraman.php
文件复制到应用程序的配置目录。
路由
Laraman 使用路由中的 resource
方法构建所有必需的路由。
在 routes/web.php
中添加"
Laraman::resource('users');
Laraman 将现在查找 app/Http/Controllers/Manage/UserController.php
。
可以在 config/laraman.php
文件中更改 Laraman 控制器的命名空间。Manage
是默认命名空间。
模型
在您的模型上包含 Laraman 特性
use Christhompsontldr\Laraman\Traits\LaramanModel;
然后使用它
use LaramanModel;
Laraman 使用我们称之为 formatters
的东西。我们包含了一些默认格式化程序,但您也可以编写自己的。请参阅 Christhompsontldr\Laraman\Traits\LaramanModel
类的示例。
将这些视为后访问器。这允许 Laraman 在应用程序的访问器应用后操纵模型数据。
使用日期格式化程序的示例
public function __configure()
{
$this->columns = [
[
'field' => 'created_at',
'display' => 'Created',
'formatter' => 'datetime',
'options' => [
'format' => 'F j, Y g:ia',
]
],
控制器
在您的控制器上包含 Laraman 特性
use Christhompsontldr\Laraman\Traits\LaramanController;
然后使用它
use LaramanController;
Laraman 预期您的控制器有一个 __configure()
方法,其中配置了一些内容。
public function __configure()
{
$this->columns = [
[
'field' => 'id',
],
[
'field' => 'name',
],
[
'field' => 'email',
],
[
'field' => 'organization.name',
'display' => 'Organization',
],
];
$this->buttons = [
config('laraman.view.hintpath') . '::buttons.view',
];
}
此示例将构建一个包含 4 列和 1 个按钮的表格索引路由。
选项
模型
如果您想使用的模型名称不符合您为控制器使用的命名约定,可以使用模型属性进行设置
public function __configure()
{
$this->model = \App\Mail::class;
视图
需要从另一个路径加载视图,请使用 viewPath
属性
public function __configure()
{
$this->viewPath = config('laraman.view.hintpath') . '::mail';
路由
可以更改此控制器中 Laraman 存在的路由
public function __configure()
{
$this->routePath = config('laraman.route.prefix') . '.mail';
搜索
可以使用 searchEnabled
属性启用模型级搜索
public function __configure()
{
$this->searchEnabled = true;
您的模型需要实现一个 search()
方法。这通常可以在 Laravel Scout 库或 Algolia Search for Laravel 库中找到。
列
列的唯一必需数组键是 field
。这将是要显示的数据库列名。
显示
display
将更改显示在表格顶部的名称。
相关模型数据
可以使用点符号来访问相关模型数据。
public function __configure()
{
$this->columns = [
[
'field' => 'id',
],
[
'field' => 'name',
],
[
'field' => 'email',
],
[
'field' => 'organization.name',
'display' => 'Organization',
],
];
organization.name
将从相关组织加载名称。
blade
如果您需要为字段使用自定义 blade,请按如下方式定义它
public function __configure()
{
$this->columns = [
[
'field' => 'braintree_customer_id',
'display' => 'Braintree Customer',
'options' => [
'blade' => config('laraman.view.hintpath') . '::fields.memberships.customer'
]
],
过滤器
Laraman 可以利用模型上定义的过滤器
public function __configure()
{
$this->filters = [
[
'field' => 'event',
'display' => 'Event',
'type' => 'select',
'values' => [
'send' => 'send',
'hard_bounce' => 'hard bounce',
'open' => 'open',
'soft_bounce' => 'soft bounce',
'deferral' => 'clickdeferral',
'delivered' => 'delivered',
'reject' => 'reject',
'spam' => 'spam',
]
],
];
如果模型有一个 filterEvent
定义,它将被利用
public function filterEvent($builder, $val)
{
return $builder->{$val}();
}
可以用来应用模型作用域,如 scopeSend()
和 scopeOpen()
。
按钮
可以使用 buttons
属性添加操作按钮
public function __configure()
{
$this->buttons = [
'laraman::buttons.braintree-transaction',
'laraman::buttons.receipt',
];
作用域
如果您需要限制使用的模型,请在控制器中定义一个 scope
方法
class TrialController extends Controller
{
use LaramanController;
public function scope($builder)
{
// only show trials
return $builder->trial();
}
附加内容
从控制器传递额外数据到视图,使用 extras
class TrialController extends Controller
{
use LaramanController;
public function __configure()
{
$this->columns = [
[
'field' => 'created_at',
'display' => 'Created',
'formatter' => 'datetime',
'options' => [
'format' => 'F j, Y g:ia',
]
]
];
// active trials
$this->extras['active'] = Membership::trial()->active()->count();
// by day
$this->extras['byday'] = [];
foreach (range(0, 30) as $day) {
$date = Carbon::now()->subDays($day)->format('Y-m-d');
$this->extras['byday'][$date] = Membership::trial()->active()->whereDate('created_at', $date)->count();
}
}