SQLSTATE[HY000]:一般错误:1364 字段“名称”没有默认值

问题描述 投票:0回答:1

我正在使用 laravel/breeze 和 stancl/tenancy 构建一个基于 laravel8 的多租户应用程序。当我尝试添加租户时,它显示“SQLSTATE[HY000]:一般错误:1364 字段“名称”没有默认值...”。我尝试在 config/database.php 中添加 default('')、nullable() 并设置 strict=false,这不会返回任何错误,但所有这些操作都会向数据库中的租户表发送空值。我检查了 usung dd() ,它返回了我通过表单提交的数据。如果我这里是我正在使用的代码结构-

架构

Schema::create('tenants', function (Blueprint $table) {
  $table->string('id')->primary();

  $table->string('name');
  $table->string('email');
  $table->string('password');

  $table->timestamps();
  $table->json('data')->nullable();
});

型号

class Tenant extends BaseTenant implements TenantWithDatabase
{
    use HasDatabase, HasDomains;

    protected $guarded = [
        'id'
    ];
    
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    public static function getCustomValue(): array {
        return [
            'id',
            'name',
            'email',
            'password'
        ];
    }

    public function setPasswordAttribute($value){
        return $this->attributes['password'] = bcrypt($value);
    }
}

控制器

public function store(Request $request)
{
    // Validation
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|email|max:255',
        'domain_name' => 'required|string|max:255|unique:domains,domain',
        'password' => ['required', 'confirmed', Rules\Password::defaults()],
    ]);

    // dd($validatedData)->toArray();

    $tenant = Tenant::create($validatedData);

    $tenant->domains()->create([
        'domain' => $validatedData['domain_name'].'.'.config('app.domain')
    ]);

    return redirect()->route('tenants.index');
}

表格


<!-- Validation Errors -->
<x-auth-validation-errors class="mb-4" :errors="$errors" />

<form method="POST" action="{{ route('tenants.store') }}">
    @csrf

    <!-- Name -->
    <div>
        <x-label for="name" :value="__('Name')" />

        <x-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus />
    </div>

    <!-- Domain Name -->
    <div class="mt-4">
        <x-label for="domain_name" :value="__('Domain Name')" />

        <x-input id="domain_name" class="block mt-1 w-full" type="text" name="domain_name" :value="old('domain_name')" required autofocus />
    </div>

    <!-- Email Address -->
    <div class="mt-4">
        <x-label for="email" :value="__('Email')" />

        <x-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required />
    </div>

    <!-- Password -->
    <div class="mt-4">
        <x-label for="password" :value="__('Password')" />

        <x-input id="password" class="block mt-1 w-full" type="password" name="password" required autocomplete="new-password" />
    </div>

    <!-- Confirm Password -->
    <div class="mt-4">
        <x-label for="password_confirmation" :value="__('Confirm Password')" />

        <x-input id="password_confirmation" class="block mt-1 w-full" type="password" name="password_confirmation" required />
    </div>

    <div class="flex items-center justify-end mt-4">
        <x-button class="ml-4">
            {{ __('Create') }}
        </x-button>
    </div>
</form>

我需要插入到数据库表中的值而不是表中的空值。

laravel laravel-8 multi-tenant laravel-breeze tenancyforlaravel
1个回答
0
投票

这里的错误是使用了错误的函数。我在模型中使用了

getCustomValue()
,应该是
getCustomColumns()
。替换函数名称后一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.