relations table get data

i have 2 selections supplier and brand. in supplier table i have brand_id column i want when i choose supplier brand selection should shows me only brands which is in supplier brand_id have. i trying to do it using option and filter it but its not works, there is my code
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->searchable()
->required()
->preload()

]),
Select::make('brand_id')
->label('Brand')
->relationship('brand', 'name')
->searchable()
->required()
->preload()
->options(function () {
$supplierBrandIds = Supplier::pluck('brand_id')->toArray();
return Brand::whereIn('id', $supplierBrandIds)->get()->pluck('name', 'id');
})

->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
]),
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->searchable()
->required()
->preload()

]),
Select::make('brand_id')
->label('Brand')
->relationship('brand', 'name')
->searchable()
->required()
->preload()
->options(function () {
$supplierBrandIds = Supplier::pluck('brand_id')->toArray();
return Brand::whereIn('id', $supplierBrandIds)->get()->pluck('name', 'id');
})

->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
]),
19 Replies
Tetracyclic
Tetracyclic7mo ago
When you're using relationship() with a Select you should use the third parameter on that method to alter the query, not options() https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-relationship-query
gigiloouu
gigiloouu7mo ago
thanks 🙏
Tetracyclic
Tetracyclic7mo ago
You'll also want to make the supplier_id Select live, so that you can update that query whenever it changes.
gigiloouu
gigiloouu7mo ago
i remaind reminded* im trying to do this about 2 hours dd can u help me.. i wrote many querys but almost none work @Tetracyclic
Tetracyclic
Tetracyclic7mo ago
Is brand_id on Supplier just a reference to a single Brand?
gigiloouu
gigiloouu7mo ago
no look in supplier table i have brand_id which is string and have values 1,2,3,4 many brands ids i mean
gigiloouu
gigiloouu7mo ago
f
No description
gigiloouu
gigiloouu7mo ago
like that and i have brand table and i want when i choose this Marksfafa supplier brand selection should be only 1,2,3 ids brands
Tetracyclic
Tetracyclic7mo ago
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->searchable()
->required()
->preload()
->live()
]),

Select::make('brand_id')
->relationship('brand', 'name', function (Builder $query, Get $get) {
$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id)
$query->whereIn('id', $brandIds)
})
->hidden(fn (Get $get) => $get('supplier_id') === null),

->label('Brand')
->searchable()
->required()
->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
]),
Select::make('supplier_id')
->label('Supplier')
->relationship('supplier', 'name')
->searchable()
->required()
->preload()
->live()
]),

Select::make('brand_id')
->relationship('brand', 'name', function (Builder $query, Get $get) {
$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id)
$query->whereIn('id', $brandIds)
})
->hidden(fn (Get $get) => $get('supplier_id') === null),

->label('Brand')
->searchable()
->required()
->createOptionForm([
TextInput::make('name')
->required()
->label('New Brand'),
]),
gigiloouu
gigiloouu7mo ago
oh thanks bro ❤️
Tetracyclic
Tetracyclic7mo ago
You probably want something like that, you'll need to use explode() to split your string of IDs up. It might make more sense to do that on the Supplier model itself, or better yet, model the Supplier > Brand relationship as a many-to-many, rather than using a string of IDs.
gigiloouu
gigiloouu7mo ago
can u show me Builder import?
Tetracyclic
Tetracyclic7mo ago
It would be Illuminate\Database\Eloquent\Builder, but you could find that out by running the code and seeing what fails. 😉 This is all in the docs.
gigiloouu
gigiloouu7mo ago
i know im trying to do this about 2 hours but now i have error
$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id);
$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id);
Attempt to read property "brand_id" on null i think problem is $get('supplier_id'))->brand_id) there i have not supplier_id in supplier table i have brand_id but main errori s brand_id is null it can read it
Tetracyclic
Tetracyclic7mo ago
$get('supplier_id') is retrieving the selected Supplier ID from the first select. You will need to add a check to make sure it's set before doing this.
->relationship('brand', 'name', function (Builder $query, Get $get) {
if($get('supplier_id') === null) {
return;
}

$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id)
$query->whereIn('id', $brandIds)
})
->relationship('brand', 'name', function (Builder $query, Get $get) {
if($get('supplier_id') === null) {
return;
}

$brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id)
$query->whereIn('id', $brandIds)
})
Want results from more Discord servers?
Add your server