SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value

121 Views Asked by At

I'm building a multitenant application with laravel8 using laravel/breeze and stancl/tenancy. When I try to add a tenant it shows "SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value ...". I've tried adding default(''), nullable() and setting strict=false in config/database.php which doesn't return any error but all these actions sending empty value to the tenant table in the database. I checked usung dd() and it's returning the data I'm submitting through the form. If I Here're the code structure I'm using-

Schema

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();
});

Model

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);
    }
}

Controller

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');
}

Form


<!-- 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>

I need the values inserted to the database table rather than empty values in the table.

1

There are 1 best solutions below

0
Mazumder Nazmul On

The mistake here is using the wrong function. I used getCustomValue() in the Model which should be getCustomColumns(). After replacing the function name everything works fine.