Filament Multi Tenant app CreateOptionForm

Hello, I am developing my multi-tenant app and want to use quick add using createOptionForm. This below is my create option forms.
Select::make('department_id')
->relationship('department', 'name', fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()))
->native(false)
->required()
->createOptionForm([
TextInput::make('company_id')
->numeric()
->default(Filament::getTenant()->id),
TextInput::make('name')
->label('Department Name')
->required()
->columnSpanFull(),
]),
Select::make('title_id')
->relationship('title', 'name', fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()))
->native(false)
->required()
->createOptionForm([
TextInput::make('company_id')
->numeric()
->default(Filament::getTenant()->id),
TextInput::make('name')
->label('Title Name')
->required()
->columnSpanFull(),
]),
Select::make('department_id')
->relationship('department', 'name', fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()))
->native(false)
->required()
->createOptionForm([
TextInput::make('company_id')
->numeric()
->default(Filament::getTenant()->id),
TextInput::make('name')
->label('Department Name')
->required()
->columnSpanFull(),
]),
Select::make('title_id')
->relationship('title', 'name', fn (Builder $query) => $query->whereBelongsTo(Filament::getTenant()))
->native(false)
->required()
->createOptionForm([
TextInput::make('company_id')
->numeric()
->default(Filament::getTenant()->id),
TextInput::make('name')
->label('Title Name')
->required()
->columnSpanFull(),
]),
this works perfectly fine but I dont want to show the company_id field. When I also give property
->visible(false)
->visible(false)
it throws me error that company_id constraint cannot be null. Can you point me to the right direction? Thanks a lot in advance
Solution:
It was like the first line below. But in Laravel docs, it is like the second line "with brackets" ```php #[ObservedBy(TitleObserver::class)] #[ObservedBy([TitleObserver::class])]...
Jump to solution
4 Replies
blntgvn42
blntgvn423mo ago
And I have also tried observers but still get company_id not null constraint
// Observer
class TitleObserver
{
public function creating(Title $title): void
{
$title->company_id = Filament::getTenant()->id;
}
}

// Model

#[ObservedBy(TitleObserver::class)]
class Title extends Model
{
use HasFactory;

public function company()
{
return $this->belongsTo(Company::class);
}

public function employees()
{
return $this->hasMany(User::class);
}
}
// Observer
class TitleObserver
{
public function creating(Title $title): void
{
$title->company_id = Filament::getTenant()->id;
}
}

// Model

#[ObservedBy(TitleObserver::class)]
class Title extends Model
{
use HasFactory;

public function company()
{
return $this->belongsTo(Company::class);
}

public function employees()
{
return $this->hasMany(User::class);
}
}
Povilas K
Povilas K3mo ago
Blind guess: maybe your company_id field is not in the $fillable array of Title model?
blntgvn42
blntgvn423mo ago
I'm using Model::unguard() for development purposes in AppServiceProvider Yee. Found the problem.
Solution
blntgvn42
blntgvn423mo ago
It was like the first line below. But in Laravel docs, it is like the second line "with brackets"
#[ObservedBy(TitleObserver::class)]
#[ObservedBy([TitleObserver::class])]
#[ObservedBy(TitleObserver::class)]
#[ObservedBy([TitleObserver::class])]
Want results from more Discord servers?
Add your server
More Posts