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
19 Replies
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-querythanks 🙏
You'll also want to make the
supplier_id
Select live, so that you can update that query whenever it changes.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
Is
brand_id
on Supplier
just a reference to a single Brand
?no look in supplier table i have brand_id which is string and have values 1,2,3,4 many brands ids i mean
f
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
oh thanks bro ❤️
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.can u show me Builder import?
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.i know im trying to do this about 2 hours
but now i have error
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
$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.
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..
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.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 ❤️ 😄
Glad you got it sorted.