SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
Hello everyone I ran into a problem during the implementation of Multi-tenancy, I tried to do everything according to the documentation, but apparently I made a mistake somewhere. RegisterCompany.php :
Company model :
User model :
<?php
// use ...
class RegisterCompany extends RegisterTenant
{
public static function getLabel(): string
{
return 'Create company';
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function form(Form $form): Form
{
return $form
->schema([
Select::make('type')
->options([
'General' => 'General',
'Simplified' => 'Simplified',
'Patent' => 'Patent'
]),
TextInput::make('name'),
TextInput::make('short_name'),
]);
}
protected function handleRegistration(array $data): Company
{
$company = Company::create($data);
$company->members()->attach(auth()->user());
return $company;
}
}
<?php
// use ...
class RegisterCompany extends RegisterTenant
{
public static function getLabel(): string
{
return 'Create company';
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function form(Form $form): Form
{
return $form
->schema([
Select::make('type')
->options([
'General' => 'General',
'Simplified' => 'Simplified',
'Patent' => 'Patent'
]),
TextInput::make('name'),
TextInput::make('short_name'),
]);
}
protected function handleRegistration(array $data): Company
{
$company = Company::create($data);
$company->members()->attach(auth()->user());
return $company;
}
}
public function members():BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function Requisite() : HasOne
{
return $this->hasOne(CompanyRequisite::class);
}
public function members():BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function Requisite() : HasOne
{
return $this->hasOne(CompanyRequisite::class);
}
User model :
public function getTenants(Panel $panel): array|\Illuminate\Support\Collection
{
return $this->Companies;
}
public function Companies(): BelongsToMany
{
return $this->belongsToMany(Company::class);
}
public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}
public function getTenants(Panel $panel): array|\Illuminate\Support\Collection
{
return $this->Companies;
}
public function Companies(): BelongsToMany
{
return $this->belongsToMany(Company::class);
}
public function canAccessTenant(Model $tenant): bool
{
return $this->companies->contains($tenant);
}
7 Replies
up
can you see the error logs and which model casing this error or what table is it?
I am guessing you missing a fillable? check
members
model tooLogs : When I create a company, the user_id is not filled in, although everything is written correctly in RegisterCompany:
I also attach the Company model with the members function :
[previous exception] [object] (PDOException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value at C:\\Users\\crash\\OneDrive\\Рабочий стол\\FinTrack\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:580)
[stacktrace]
[previous exception] [object] (PDOException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value at C:\\Users\\crash\\OneDrive\\Рабочий стол\\FinTrack\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:580)
[stacktrace]
<?php
namespace App\Filament\Pages\Tenancy;
use App\Models\Company;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Tenancy\RegisterTenant;
use Illuminate\Database\Eloquent\Model;
class RegisterCompany extends RegisterTenant
{
public static function getLabel(): string
{
return 'Create company';
}
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name'),
TextInput::make('short_name'),
Select::make('type')
->options([
'General' => 'General',
'Simplified' => 'Simplified',
'Patent' => 'Patent'
])
]);
}
protected function handleRegistration(array $data): Company
{
$company = Company::create($data);
$company->members()->attach(auth()->user());
return $company;
}
}
<?php
namespace App\Filament\Pages\Tenancy;
use App\Models\Company;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Tenancy\RegisterTenant;
use Illuminate\Database\Eloquent\Model;
class RegisterCompany extends RegisterTenant
{
public static function getLabel(): string
{
return 'Create company';
}
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name'),
TextInput::make('short_name'),
Select::make('type')
->options([
'General' => 'General',
'Simplified' => 'Simplified',
'Patent' => 'Patent'
])
]);
}
protected function handleRegistration(array $data): Company
{
$company = Company::create($data);
$company->members()->attach(auth()->user());
return $company;
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
class Company extends Model
{
protected $fillable = [
'user_id',
'name',
'short_name',
'type',
];
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
use HasFactory, SoftDeletes;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
class Company extends Model
{
protected $fillable = [
'user_id',
'name',
'short_name',
'type',
];
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
use HasFactory, SoftDeletes;
}
it's not clear from the error what table is it
can you expand the error page and see the other trace
also make sure you have this where you need them maybe in Company model
protected $fillable = [
'user_id',
];
protected $fillable = [
'user_id',
];
Maybe this will help somehow:
Also, because of what I did according to the guide, it is worth specifying the migration of the companies table :
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
INSERT INTO
`companies` (
`name`,
`short_name`,
`type`,
`updated_at`,
`created_at`
)
VALUES
(
asdasd,
asdasd,
Simplified,
2024 -01 -17 19: 41: 09,
2024 -01 -17 19: 41: 09
)
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
INSERT INTO
`companies` (
`name`,
`short_name`,
`type`,
`updated_at`,
`created_at`
)
VALUES
(
asdasd,
asdasd,
Simplified,
2024 -01 -17 19: 41: 09,
2024 -01 -17 19: 41: 09
)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('short_name');
$table->string('type');
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->softDeletes();
});
Schema::create('company_user', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('company_user');
Schema::dropIfExists('companies');
}
};
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('short_name');
$table->string('type');
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->softDeletes();
});
Schema::create('company_user', function (Blueprint $table) {
$table->id();
$table->foreignId('company_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('company_user');
Schema::dropIfExists('companies');
}
};
Solution
yes
so you have to add
INSERT INTO
`companies` (
`name`,
`short_name`,
`type`,
`updated_at`,
`created_at`
)
INSERT INTO
`companies` (
`name`,
`short_name`,
`type`,
`updated_at`,
`created_at`
)
user_id
to the $data
and make sure its fillable
in the model company
$data['user_id'] = auth()->user()->id
$company = Company::create($data);
$data['user_id'] = auth()->user()->id
$company = Company::create($data);
I'm shocked! I just don't have the words to be honest. The fact is that everything worked fine before migrate:refresh, but after that this error appeared and I tried to fix it for more than 3 hours. Thank you so much for helping such complete beginners in filament like me, you are a pro!❤️