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.
43 Replies
What's your goal to achieve?
If you can edit your database then delete "department_name", and have only "department_id". In form you can do something like
this will store department id in your table. And you can display it like this in table
Tables\Columns\TextColumn::make('department.name')
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
should do it for you
relationship($relation, $titleAttribute)
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()
thanks for your reply @alexandergaal , I'm want to use TextInput not select.
I'd recommend a placeholder, give me one sec.
OMG π
TextInput is misleading UX imo
@Vp thanks for your reply, I think I will make it one field if the name is getting displayed by relationship with department_id .
In select YES, in text input NO, but alex suggestions for placeholder can do the trick
Not sure if I understood the requirement 100% tbf
me 2
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.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.
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.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 anythingI don't know your models, so not sure if
$record->department->name
would be correct.Placeholder::make('department_id')
->label(__('Department'))
->content(fn (?Department $record) => $record?->department?->name),
@leoPascal can you post your owner select and your model relationship
$record->owner?->department?->name
would be correct then I guess<?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.Okay π
It's personal choice, but you can also do like this
Base on your owner select, I think you need to check more condition like
->where('owner_id', $get('owner_id'))
tried with this, got this error "Call to a member function value() on null"
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 wrongApologies if my questions seem silly, and if I come across as sounding dumb / less knowledgeable.
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. π
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
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?do I have to use live() on owner select?
No, in v2 reactive() is enough
ohh ok
yes everything working fine except this in the placeholder dd($get('owner_id'));
Maybe placeholder content don't support this.. i'm off work rn, cannot check docs
Can you try textinput placeholder..
sorry, I wasn't aware you are using v2
will check and let you guys know. @alexandergaal , @Vp Thank you so very much for your kind help.
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."
no problem π
@alexandergaal Please check, if you can help with this π .
I canβt look into it rn :/
@alexandergaal no issues at all, whenever you have time. Thank you so much.