laravel SQLSTATE[HY000]:一般错误:1364 1364 Field 'name' doesn't have a default value

efzxgjgh  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(262)

我正在使用laravel/breeze和stancl/tenancy构建一个laravel 8的多租户应用程序。当我尝试添加租户时,它显示“SQLSTATE[HY 000]:一般错误:1364 1364栏位'name'没有预设值。我试过添加default(''),nullable()和设置strict=false在MySQL/database.php中,这不会返回任何错误,但所有这些操作都会向数据库中的租户表发送空值。我检查了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>

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

bq9c1y66

bq9c1y661#

这里的错误是使用了错误的功能。我在模型中使用了getCustomValue(),它应该是getCustomColumns()。替换函数名后,一切正常。

相关问题