weird select option behavior on relationship
i have this select option
when am in create form no data is displayed in the select options and on edit the name of the customer can bee seen as label
on the otherhand if I comment the relationship function the opposite happens in create the select option is ok but in edit i see the id and not the name I need help Please
Forms\Components\Select::make('customer_id')
->label('Customer')->live(debounce: 300)
->required()->searchable()
->relationship('customer', ignoreRecord: true)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
})
->options(function (Forms\Get $get) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = Customer::whereHas('fcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = Customer::whereHas('lcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = Customer::whereHas('clearance_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
})Forms\Components\Select::make('customer_id')
->label('Customer')->live(debounce: 300)
->required()->searchable()
->relationship('customer', ignoreRecord: true)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
})
->options(function (Forms\Get $get) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = Customer::whereHas('fcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = Customer::whereHas('lcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = Customer::whereHas('clearance_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
})when am in create form no data is displayed in the select options and on edit the name of the customer can bee seen as label
on the otherhand if I comment the relationship function the opposite happens in create the select option is ok but in edit i see the id and not the name I need help Please
Solution
this is still making no difference but Thank God i figured out something that solved my problem and this is what i did
in edit I see the name and in create I see my customers with certain conditions.
And thank you guys for taking time to solve this too.
Forms\Components\Select::make('customer_id')
->label('Customer')->live()
->required()->searchable()->preload()
->relationship(name: 'customer', titleAttribute: 'name',ignoreRecord: true)
->options(function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = Customer::whereHas('fcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = Customer::whereHas('lcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = Customer::whereHas('clearance_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
}) Forms\Components\Select::make('customer_id')
->label('Customer')->live()
->required()->searchable()->preload()
->relationship(name: 'customer', titleAttribute: 'name',ignoreRecord: true)
->options(function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = Customer::whereHas('fcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = Customer::whereHas('lcl_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = Customer::whereHas('clearance_expense', function ($query) {
$query->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
})in edit I see the name and in create I see my customers with certain conditions.
And thank you guys for taking time to solve this too.