百万 / 自动填充
此包已被弃用且不再维护。未建议替代包。
一个 Laravel Nova 字段。
v1.4
2020-05-23 11:11 UTC
Requires
- php: >=7.1.0
README
nova-autofill 是一个 Laravel Nova 包,允许自动填充 Nova 表单字段。
安装
composer require Miljoen/nova-autofill
为新的 Laravel 项目示例用法。
-
使用以下命令安装包
composer require Miljoen/nova-autofill
-
将
use AutofillTrait;
添加到/app/User.php
模型中。 -
实现
filterKey()
和autofillModels()
方法。例如
function filterKey(): string { return 'email'; } function autofillModels(): \Illuminate\Support\Collection { return collect([ new \App\User(['name' => 'cedric', 'email' => 'cedric@cedric.cedric', 'password' => \Illuminate\Support\Facades\Hash::make('cedric')]), new \App\User(['name' => 'yoeri', 'email' => 'yoeri@yoeri.yoeri', 'password' => \Illuminate\Support\Facades\Hash::make('yoeri')]) ]); }
-
将
Autofill::make('Nova Autofill', 'id')->options( \App\User::filterKey(), \App\User::autofillModels()),
添加到
/app/Nova/User.php
中fields()
函数的返回值中 -
在浏览器中转到
.../nova/resources/users/new
路由以测试包
/app/User.php
模型应如下所示
<?php
namespace App;
use AutofillTrait;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
use AutofillTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
static function filterKey(): string
{
return 'email';
}
static function autofillModels(): \Illuminate\Support\Collection
{
return collect([
new \App\User(['name' => 'cedric', 'email' => 'cedric@cedric.cedric', 'password' => \Illuminate\Support\Facades\Hash::make('cedric')]),
new \App\User(['name' => 'yoeri', 'email' => 'yoeri@yoeri.yoeri', 'password' => \Illuminate\Support\Facades\Hash::make('yoeri')])
]);
}
}
/app/nova/User.php
的 fields()
函数应如下所示
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
Autofill::make('Nova Autofill', 'id')->options(
\App\User::filterKey(),
\App\User::autofillModels()
),
Gravatar::make()->maxWidth(50),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password')
->onlyOnForms()
->creationRules('required', 'string', 'min:8')
->updateRules('nullable', 'string', 'min:8'),
];
}