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
Tetracyclic10mo 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
gigiloouuOP10mo ago
thanks 🙏
Tetracyclic
Tetracyclic10mo ago
You'll also want to make the supplier_id Select live, so that you can update that query whenever it changes.
gigiloouu
gigiloouuOP10mo 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
Tetracyclic10mo ago
Is brand_id on Supplier just a reference to a single Brand?
gigiloouu
gigiloouuOP10mo 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
gigiloouuOP10mo ago
f
No description
gigiloouu
gigiloouuOP10mo 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
Tetracyclic10mo 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
gigiloouuOP10mo ago
oh thanks bro ❤️
Tetracyclic
Tetracyclic10mo 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
gigiloouuOP10mo ago
can u show me Builder import?
Tetracyclic
Tetracyclic10mo 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
gigiloouuOP10mo 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
Tetracyclic10mo 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)
})
gigiloouu
gigiloouuOP10mo ago
now problem only is when i select supplier and click brand i have this err explode(): Argument #2 ($string) must be of type string, array given in there $brandIds = explode(',', Supplier::find($get('supplier_id'))->brand_id); we use explode right so why..
Tetracyclic
Tetracyclic10mo ago
Do you have an attribute that is casting brand_id to an array? If so you shouldn't need explode() there, but you said it was a string above.
gigiloouu
gigiloouuOP10mo ago
yes in supplier model i have this code public function brandId(): Attribute { return Attribute::make( get: fn($value) => explode(',', $value), set: fn($value) => implode(',', $value), ); } public function subbrandId(): Attribute { return Attribute::make( get: fn($value) => explode(',', $value), set: fn($value) => implode(',', $value), ); } aa its works finallly thanks u soo much ❤️ i take a look code and again thanks ❤️ 😄
Tetracyclic
Tetracyclic10mo ago
Glad you got it sorted.
Want results from more Discord servers?
Add your server