Home > Mobile >  Is it possible to organize data from a column separated by dash in blade of laravel 8?
Is it possible to organize data from a column separated by dash in blade of laravel 8?

Time:05-25

I wanted to clear this doubt, in the column of my table (variant) I am saving the values separated by a dash (-), in the display of these values on the page in the product I wanted to know if it is possible to organize them by looking for the first value before the dash and the value after the dash put it in second option as in the example below:

Mysql

White-S
White-M
Black-XS
Black-XL

to

Front site

first input radio-> White
sub input radio-> S
sub input radio-> M

first input radio-> Black
sub input radio-> XS
sub input radio-> XL

CodePudding user response:

$data=collect([
    (object)['dash_grouped'=>'White-S','other'=>1],
    (object)['dash_grouped'=>'White-M','other'=>2],
    (object)['dash_grouped'=>'Black-XS','other'=>3],
    (object)['dash_grouped'=>'Black-XL','other'=>4],
]);

If you have the data in a collection in a similar way, you can just extract the parts by script using explode

$dataSplit=$data
    ->each(function($d){
         $parts=explode('-',$d->dash_grouped);
         $d->color=$parts[0];
         $d->size=$parts[1];
    });

And then in the frontend group the data by the new attribute. This can also be done in the Controller Previously.

    @foreach($dataSplit->groupBy('color') as $brandGroupName=>$brand)
        <p>{{$brandGroupName}}</p>
        <ul>
            @foreach($brand as $types)
                <li>sub input radio-> {{$types->size}}</li>
            @endforeach
        </ul>
    @endforeach

Example in Frontend

  • Related