How can I show all tags from product table in laravel, when all tags are in products table? [closed]

This is an eCommerce project, and I stored product tags in the product table. In the database, all tags are stored separated by commas. But how can I show all tags in view using groupBy ??

Here is my database:

Answer

If the database was normalized, the tags would be in their own table. But since you have them like this, I assume you’re getting the products with something like:

$products = Product::get();

If so, you can still get all different tags by using some collection methods.

$tags_en = $products->map(fn($product) => explode(',', $product->product_tag_en))
                    ->flatten()
                    ->unique()   // avoid repeating tags
                    ->sort()     // optional
                    ->values();

$tags_bn = $products->map(fn($product) => explode(',', $product->product_tag_bn))
                    ->flatten()
                    ->unique()   // avoid repeating tags
                    ->sort()     // optional
                    ->values();