Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kazzuya's avatar

no results

Hello, I have 3 tables like that:

sub_category(id , sub_category_name) product_filters(id, filter_name , sub_category_id) filter_value(id, filter_value_name, product_filter_id)

When i choose a subCategory i need to show the related filter_name , and filter_value_name, The sub_category_id in product_filters is varchar , in in database it will be like that: sub_category_id : 1,2,3 And i have the relations in models, I have this code:

public $sub;
public $productFilter;
public $sub_cat;

public function mount(Subcategory $sub, ProductFilter $productFilter)
{
    $this->sub = $sub;
    $this->productFilter = $productFilter;
    $this->sub_cat = explode(",", $this->productFilter->sub_category_id);
}

    public function getFilter()
	{
    $filters = ProductFilter::join('product_filter_values', 'product_filters.id', '=', 				 
 		'product_filter_values.product_filter_id')
    ->join('sub_categories', 'sub_categories.id', '=', 'product_filters.sub_category_id')
    ->select('product_filters.filter_name', 'product_filter_values.filter_value')
    ->where('sub_categories.id',  $this->sub->id)
    ->get()
    ->groupBy('filter_name')
    ->map(function ($values) {
        return $values->pluck('filter_value');
    });

return $filters;
}

But there is no result tried dd($this->sub_cat) but i got 0

0 likes
5 replies
bestmomo's avatar

Hello,

Looks like you are using explode on an integer :

$this->sub_cat = explode(",", $this->productFilter->sub_category_id);
kazzuya's avatar

@bestmomo this sub_category_id is not int it's varchar to store multiple value like this: 3,1,5 And i have made the relation in models with sub_category table

kazzuya's avatar

@Tray2 not a real project , testing something like filtering, The admin can alter on product table from hes dashboard to add this filter_name And this filter like brand can be for multiple sub_category I know it will be smthf like injection i think.

If u have another way i will be thankfull.

jlrdw's avatar

@kazzuya

$this->sub_cat = explode(",", $this->productFilter->sub_category_id);

You get an array. But what do you want to do with it.

I suggest working some of the examples in the documentation to get a better understanding of using related data.

Please or to participate in this conversation.