weird select option behavior on relationship

i have this select option
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 ```php Forms\Components\Select::make('customer_id') ->label('Customer')->live() ->required()->searchable()->preload()...
Jump to solution
12 Replies
Adnan Yalahow
Adnan YalahowOPβ€’6mo ago
can someone provide solution for me please any volunteers please πŸ™
Tally
Tallyβ€’6mo ago
like Adam said in another post: "Options and relationship don’t work together. One will always override the other."
Adnan Yalahow
Adnan YalahowOPβ€’6mo ago
so how do i prevent that i need to get customers using a relationship while showing the name in edit
Adnan Yalahow
Adnan YalahowOPβ€’6mo ago
Ihave done this
Forms\Components\Select::make('customer_id')
->label('Customer')->live(debounce: 300)
->required()->searchable()
->relationship(
name: 'customer',
titleAttribute: 'name',
modifyQueryUsing: function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = $query->whereHas('fcl_expense', function ($fclQuery) {
$fclQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = $query->whereHas('lcl_expense', function ($LCLQuery) {
$LCLQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = $query->whereHas('clearance_expense', function ($clQuery) {
$clQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
},
ignoreRecord: true,
)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
}),
Forms\Components\Select::make('customer_id')
->label('Customer')->live(debounce: 300)
->required()->searchable()
->relationship(
name: 'customer',
titleAttribute: 'name',
modifyQueryUsing: function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
$data = [];
if ($get('related_to') === 'fcl job') {
$data = $query->whereHas('fcl_expense', function ($fclQuery) {
$fclQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'lcl job') {
$data = $query->whereHas('lcl_expense', function ($LCLQuery) {
$LCLQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
} elseif ($get('related_to') === 'clearance') {
$data = $query->whereHas('clearance_expense', function ($clQuery) {
$clQuery->where('status', 'pending')->where('stage', 'approved');
})->get();
}
return $data->pluck('name', 'id');
}
},
ignoreRecord: true,
)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
}),
now in edit its showing id instead of name and in create am not getting any options what am i doing wrong?
Tally
Tallyβ€’6mo ago
ray or dd the $data in your create to check the query... maybe the related_to field is null in create...
Adnan Yalahow
Adnan YalahowOPβ€’6mo ago
i tried to dd the $data before if ($get('related_to')) still nothing is shown
Tally
Tallyβ€’6mo ago
ok so the relationship is not being loaded? already tried ->preload()
Adnan Yalahow
Adnan YalahowOPβ€’6mo ago
still no change this is what i did
->relationship(
name: 'customer',
titleAttribute: 'name',
modifyQueryUsing: function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
if ($get('related_to') === 'fcl job') {
return $query->whereHas('fcl_expense', function ($fclQuery) {
$fclQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
} elseif ($get('related_to') === 'lcl job') {
return $query->whereHas('lcl_expense', function ($LCLQuery) {
$LCLQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
} elseif ($get('related_to') === 'clearance') {
return $query->whereHas('clearance_expense', function ($clQuery) {
$clQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
}
}
},
ignoreRecord: true,
)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
})
->relationship(
name: 'customer',
titleAttribute: 'name',
modifyQueryUsing: function (Forms\Get $get, Builder $query) {
if ($get('related_to')) {
if ($get('related_to') === 'fcl job') {
return $query->whereHas('fcl_expense', function ($fclQuery) {
$fclQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
} elseif ($get('related_to') === 'lcl job') {
return $query->whereHas('lcl_expense', function ($LCLQuery) {
$LCLQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
} elseif ($get('related_to') === 'clearance') {
return $query->whereHas('clearance_expense', function ($clQuery) {
$clQuery->where('status', 'pending')->where('stage', 'approved');
})->pluck('name', 'id');
}
}
},
ignoreRecord: true,
)
->getOptionLabelFromRecordUsing(function (Model $record) {
return $record['name'];
})
Tally
Tallyβ€’6mo ago
pluck returns a collection if you want to limit the fields returned by the database you probably want to use ->select()
Solution
Adnan Yalahow
Adnan Yalahowβ€’6mo ago
this is still making no difference but Thank God i figured out something that solved my problem and this is what i did
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.
Want results from more Discord servers?
Add your server