nova-autofill is a Laravel Nova package that allows for automatic filling of Nova FormFields.
composer require Miljoen/nova-autofill
-
Install the package using
composer require Miljoen/nova-autofill
-
Add
use AutofillTrait;
to the/app/User.php
Model. -
Implement the
filterKey()
andautofillModels()
methods.For example:
function filterKey(): string { return 'email'; } function autofillModels(): \Illuminate\Support\Collection { return collect([ new \App\User(['name' => 'cedric', 'email' => '[email protected]', 'password' => \Illuminate\Support\Facades\Hash::make('cedric')]), new \App\User(['name' => 'yoeri', 'email' => '[email protected]', 'password' => \Illuminate\Support\Facades\Hash::make('yoeri')]) ]); }
-
Add
Autofill::make('Nova Autofill', 'id')->options( \App\User::filterKey(), \App\User::autofillModels()),
to the return value of the
fields()
function in/app/Nova/User.php
-
Go to the
.../nova/resources/users/new
route in your browser to test the package
The /app/User.php
model should look like this:
<?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' => '[email protected]', 'password' => \Illuminate\Support\Facades\Hash::make('cedric')]),
new \App\User(['name' => 'yoeri', 'email' => '[email protected]', 'password' => \Illuminate\Support\Facades\Hash::make('yoeri')])
]);
}
}
The /app/nova/User.php
fields() function should look like this:
/**
* 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'),
];
}