百万/自动填充

此包已被弃用且不再维护。未建议替代包。

一个 Laravel Nova 字段。

安装次数2,076

依赖者: 0

建议者: 0

安全: 0

星标: 4

关注者: 1

分支: 0

开放问题: 2

语言:Vue

v1.4 2020-05-23 11:11 UTC

This package is auto-updated.

Last update: 2022-06-23 15:46:52 UTC


README

nova-autofill 是一个 Laravel Nova 包,允许自动填充 Nova 表单字段。

安装

composer require Miljoen/nova-autofill

为新的 Laravel 项目示例用法。

  1. 创建一个新的 Laravel 项目

  2. 安装 Nova

  3. 使用以下命令安装包

    composer require Miljoen/nova-autofill

  4. use AutofillTrait; 添加到 /app/User.php 模型中。

  5. 实现 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')])
        ]);
    }
    
  6. Autofill::make('Nova Autofill', 'id')->options( \App\User::filterKey(), \App\User::autofillModels()),

    添加到 /app/Nova/User.phpfields() 函数的返回值中

  7. 在浏览器中转到 .../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.phpfields() 函数应如下所示

    /**
     * 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'),
        ];
    }