F
Filamentβ€’7mo ago
jepewsykes

disabled select option multiple value base on status from db

I have Select Option Its Disabled Option value when status by id is sold Select::make('batch_id') ->required() ->relationship('batch_faktur_pajak', 'batch') ->options(BatchFakturPajak::all()->pluck('batch', 'id')) ->live(debounce: 1500) ->disableOptionWhen(function ($value) { $get_status = BatchFakturPajak::where('status', '=', 'sold')->get(); foreach ($get_status as $status) { if ($status->id == $value) { return true; } else { return false; } } }) i have 10 and 20 value bot sold status but On Select It Disabled Only 10 i know because it return bool but i dkw how to fix that
No description
No description
8 Replies
Vp
Vpβ€’7mo ago
Sth like this, but not tested
->disableOptionWhen(function ($value, $state) {
$get_status = BatchFakturPajak::where('status', '=', 'sold')->get();

return collect($get_status)
->reject(fn ($id) => $id == $state)
->filter()
->contains($value);
})
->disableOptionWhen(function ($value, $state) {
$get_status = BatchFakturPajak::where('status', '=', 'sold')->get();

return collect($get_status)
->reject(fn ($id) => $id == $state)
->filter()
->contains($value);
})
jepewsykes
jepewsykesOPβ€’7mo ago
No description
Vp
Vpβ€’7mo ago
Yeah I didn't test πŸ˜† Anyway why don't you query only (! sold) in relationship like this
->relationship(
name: 'batch_faktur_pajak',
titleAttribute: 'batch',
modifyQueryUsing: fn (Builder $query) => $query->where('status', '!=', 'sold'),
)
->relationship(
name: 'batch_faktur_pajak',
titleAttribute: 'batch',
modifyQueryUsing: fn (Builder $query) => $query->where('status', '!=', 'sold'),
)
Then you can remove options() and disableOptionWhen()
jepewsykes
jepewsykesOPβ€’7mo ago
ya this will work but i wanna to see history batch value solved btw with u suggest and little modification ->disableOptionWhen(function (int $value) { $get_status = BatchFakturPajak::where('status', 'sold')->get('id')->toArray(); return collect($get_status) ->contains(function (array $val, int $key) use ($value) { return $val['id'] == $value; }); })
jepewsykes
jepewsykesOPβ€’7mo ago
No description
jepewsykes
jepewsykesOPβ€’7mo ago
thx u bro πŸ˜†
Vp
Vpβ€’7mo ago
You're welcome. btw you can reduce your code like this
return BatchFakturPajak::where('status', 'sold')
->pluck('id')
->contains(function (array $val, int $key) use ($value) {
return $val['id'] == $value;
});
return BatchFakturPajak::where('status', 'sold')
->pluck('id')
->contains(function (array $val, int $key) use ($value) {
return $val['id'] == $value;
});
jepewsykes
jepewsykesOPβ€’7mo ago
damn that cool thx u bro πŸ˜‰
Want results from more Discord servers?
Add your server