F
Filamentβ€’15mo ago
leoPascal

how can I hide a form field which is $fillable in the model.

In my form, I have a field called department_id and I want to make it hidden() as I have another field called department_name which is displaying but disabled(). While doing so I'm getting this error "SQLSTATE[HY000]: General error: 1364 Field 'department_id' doesn't have a default value". Upon exploring this I got this "https://discord.com/channels/883083792112300104/1152158647590342696" but I'm so sorry I don't know how I can use this mutateFormDataBeforeCreate() function in my case. TextInput::make('department_id')->label('Department')->hidden(), TextInput::make('department_name')->label('Department')->disabled(),
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Solution:
Forms\Components\Select::make('department_id')
->relationship('department', 'name')
->required()
Forms\Components\Select::make('department_id')
->relationship('department', 'name')
->required()
...
Jump to solution
43 Replies
alexanderkroneis
alexanderkroneisβ€’15mo ago
What's your goal to achieve?
Vp
Vpβ€’15mo ago
If you can edit your database then delete "department_name", and have only "department_id". In form you can do something like
Forms\Components\Select::make('department_id')
->required()
->options(Department::all()->pluck('name', 'id'))
Forms\Components\Select::make('department_id')
->required()
->options(Department::all()->pluck('name', 'id'))
this will store department id in your table. And you can display it like this in table Tables\Columns\TextColumn::make('department.name')
leoPascal
leoPascalOPβ€’15mo ago
I have only department_id in the database not the department_name, but I actually want to display the department name as a auto populated field when I select the owner. This is what I'm trying to achieve.
Solution
alexanderkroneis
alexanderkroneisβ€’15mo ago
Forms\Components\Select::make('department_id')
->relationship('department', 'name')
->required()
Forms\Components\Select::make('department_id')
->relationship('department', 'name')
->required()
should do it for you
alexanderkroneis
alexanderkroneisβ€’15mo ago
relationship($relation, $titleAttribute)
Vp
Vpβ€’15mo ago
Yes, in select it will display name and it will store id using above select. If you really want another ->disabled() field to show the department_name then you can use ->live() and ->afterStateUpdated()
leoPascal
leoPascalOPβ€’15mo ago
thanks for your reply @alexandergaal , I'm want to use TextInput not select.
alexanderkroneis
alexanderkroneisβ€’15mo ago
I'd recommend a placeholder, give me one sec.
Vp
Vpβ€’15mo ago
OMG πŸ˜†
alexanderkroneis
alexanderkroneisβ€’15mo ago
Forms\Components\Placeholder::make('department.name')
->label(__('Department'))
->content(fn (?Model $record) => $record?->department?->name),
Forms\Components\Placeholder::make('department.name')
->label(__('Department'))
->content(fn (?Model $record) => $record?->department?->name),
TextInput is misleading UX imo
leoPascal
leoPascalOPβ€’15mo ago
@Vp thanks for your reply, I think I will make it one field if the name is getting displayed by relationship with department_id .
Vp
Vpβ€’15mo ago
In select YES, in text input NO, but alex suggestions for placeholder can do the trick
alexanderkroneis
alexanderkroneisβ€’15mo ago
Not sure if I understood the requirement 100% tbf
Vp
Vpβ€’15mo ago
me 2
alexanderkroneis
alexanderkroneisβ€’15mo ago
If you'd like to select a user and show the users department you'd have to change my example above a little bit by adding live() to the user select and change the function of content() to use the current form data.
leoPascal
leoPascalOPβ€’15mo ago
Anyways, I will use select. My requirement is when I select the owner the next field is department and I'm getting there department_id. I want to display here department name instead of id.
alexanderkroneis
alexanderkroneisβ€’15mo ago
Okay, got it. So my this response (https://discord.com/channels/883083792112300104/1153985748622651412/1153997600542969896) will do the job. You can replace Placeholder by TextInput as well, it's more of a UX decision.
leoPascal
leoPascalOPβ€’15mo ago
trying to use as you suggested, is it correct? Placeholder::make('department.name') ->label(__('Department')) ->content(fn (?Department $record) => $record?->department?->name), because it is not displaying anything
alexanderkroneis
alexanderkroneisβ€’15mo ago
I don't know your models, so not sure if $record->department->name would be correct.
leoPascal
leoPascalOPβ€’15mo ago
Placeholder::make('department_id') ->label(__('Department')) ->content(fn (?Department $record) => $record?->department?->name),
Vp
Vpβ€’15mo ago
@leoPascal can you post your owner select and your model relationship
leoPascal
leoPascalOPβ€’15mo ago
alexanderkroneis
alexanderkroneisβ€’15mo ago
$record->owner?->department?->name would be correct then I guess
leoPascal
leoPascalOPβ€’15mo ago
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Craft extends Model { use HasFactory; protected $fillable = [ 'owner_id', 'department_id', 'craft_type', 'craft_name', 'cg_number', 'reg_number', 'reg_date', 'expiry_date' ]; public function owner(){ return $this->belongsTo(Owner::class); } public function department(){ return $this->belongsTo(Department::class); } public function transctions(){ return $this->hasMany(Transaction::class); } } since select works fine I think better to use that instead.
alexanderkroneis
alexanderkroneisβ€’15mo ago
Okay πŸ˜„
Vp
Vpβ€’15mo ago
It's personal choice, but you can also do like this
Placeholder::make('department_id')
->label(__('Department'))
->content(function (callable $get) {
return Department::find($get('department_id'))->value('name');
})
Placeholder::make('department_id')
->label(__('Department'))
->content(function (callable $get) {
return Department::find($get('department_id'))->value('name');
})
Base on your owner select, I think you need to check more condition like ->where('owner_id', $get('owner_id'))
leoPascal
leoPascalOPβ€’15mo ago
tried with this, got this error "Call to a member function value() on null"
Vp
Vpβ€’15mo ago
don't mind about whole code, try dd($get('owner_id')) and check whether you get owner id or not. if yes, then base on that you can query department name I didn't test my code, yes it can probably wrong
leoPascal
leoPascalOPβ€’15mo ago
Apologies if my questions seem silly, and if I come across as sounding dumb / less knowledgeable.
alexanderkroneis
alexanderkroneisβ€’15mo ago
No that's fine 🫢 We all started somewhere, so I don't there should be no judgement for being at the start of a journey. πŸ™‚
leoPascal
leoPascalOPβ€’15mo ago
Thank you for your understanding and encouragement! You're absolutely right; we all begin our journeys somewhere. I'm excited to learn and grow along the way. 😊 dd($get('owner_id')) gives null
Vp
Vpβ€’15mo ago
But you have this in craft select $owners = Owner::find($get('owner_id')); πŸ˜• So, the craft select (your code) is working base on owner change?
leoPascal
leoPascalOPβ€’15mo ago
do I have to use live() on owner select?
Vp
Vpβ€’15mo ago
No, in v2 reactive() is enough
leoPascal
leoPascalOPβ€’15mo ago
ohh ok yes everything working fine except this in the placeholder dd($get('owner_id'));
Vp
Vpβ€’15mo ago
Maybe placeholder content don't support this.. i'm off work rn, cannot check docs Can you try textinput placeholder..
alexanderkroneis
alexanderkroneisβ€’15mo ago
sorry, I wasn't aware you are using v2
leoPascal
leoPascalOPβ€’15mo ago
will check and let you guys know. @alexandergaal , @Vp Thank you so very much for your kind help.
leoPascal
leoPascalOPβ€’15mo ago
I think it would be better if I use Select. @alexandergaal , @Vp thank you guys once again for your kind help. If you guys have a moment, could you please take a look at another question I posted last week https://discord.com/channels/883083792112300104/1150702532457414656 ? It hasn't received a response yet, and I would really appreciate your help with it."
alexanderkroneis
alexanderkroneisβ€’15mo ago
no problem πŸ™‚
leoPascal
leoPascalOPβ€’15mo ago
@alexandergaal Please check, if you can help with this πŸ‘† .
alexanderkroneis
alexanderkroneisβ€’15mo ago
I canβ€˜t look into it rn :/
leoPascal
leoPascalOPβ€’15mo ago
@alexandergaal no issues at all, whenever you have time. Thank you so much.
Want results from more Discord servers?
Add your server