0

Trying to figure out how I can display results for a database that has data like this...

If the data is stored like this...

 Style              Color
  1. Classy Hat - Red
  2. Classy Hat - Blue
  3. Classy Hat - Yellow
  4. Stylish Hat - Red
  5. Stylish Hat - Blue
  6. Clean Hat - Black

If I want to show the data like this

Classy Hat

Colors: Red, Blue, Yellow

Stylish Hat

Colors: Red, Blue

Clean Hat

Colors: Black

I cant figure out how to simultaneously group by as well as show the distinct traits within the group. I see examples of how to show the count of each distinct, but not how to display unique trait values for grouped records.

weekapaug
  • 332
  • 1
  • 4
  • 15
  • 1) paste if you have try some code, 2) you may to see documentation in this section:https://laravel.com/docs/5.5/eloquent-relationships and this post maybe can help you https://stackoverflow.com/questions/25847738/group-concat-laravel-eloquent – LorenzoBerti Oct 17 '17 at 15:22
  • The code I could paste wouldnt help much because its behaving as expected for a groupBy query. I was able to achieve this in other languages by writing a loop within a loop IE. Outer loop identified the grouped style and inner loop queried for the unique ID's but I cant figure out how to do the same in laravel since its all modulated and processed before display. The relationship doesnt work because its all in one table, not a relationship to a different table – weekapaug Oct 17 '17 at 15:48

1 Answers1

0

You can just fetch a normal collection from the modal, ie. Styles::all(). Then use the groupBy function on the collection.

// Sample data as per database query
$data = collect([
    ['style' => 'classy hat', 'color' => 'red'],
    ['style' => 'classy hat', 'color' => 'orange'],
    ['style' => 'classy hat', 'color' => 'green'],
    ['style' => 'stylish hat', 'color' => 'blue'],
    ['style' => 'stylish hat', 'color' => 'orange'],
]);

$styles = $data->groupBy('style');

foreach ($styles as $style => $colors) {
    echo "<h1>" . $style . "</h1>";
    echo implode(', ', $colors->pluck('color')->toArray());
}

That would give you

classy hat

red, orange, green

stylish hat

blue, orange

Alternatively you can build the whole array you need:

$styles = $data->groupBy('style')
    ->map(function ($styles) {
        return $styles->pluck('color');
    });

print_r($styles->toArray());

Array
(
[classy hat] => Array
    (
        [0] => red
        [1] => orange
        [2] => green
    )

[stylish hat] => Array
    (
        [0] => blue
        [1] => orange
    )

Update to print

foreach ($styles->toArray() as $style => $colors) {
    echo "<h1>" . $style . "</h1>";
    echo implode(', ', $colors) . "<br />";
}

Update for blade view

@foreach ($styles->toArray() as $style => $colors)
    <h1>{{ $style }}</h1>
    @foreach ($colors as $color)
        {{ $color }},
    @endforeach
    <!-- OR -->
    {{ implode(', ', $colors) }} 
@endforeach
Leon Vismer
  • 4,925
  • 1
  • 20
  • 22
  • In your second example, the code looks really clean but how can I output the name of the array as the header as you did in the first example? Same way? – weekapaug Oct 17 '17 at 16:44
  • Essentially the same way, except in the second example you only have the colors. So you could just implode the ```$colors``` or loop over them if you want. I updated to reflect the printing. – Leon Vismer Oct 17 '17 at 17:06
  • having issue with the implode and echo since laravel blade doesnt recognize standard php. I need to use the {{ $variable }} syntax and I'm having an issue figuring out how to incorporate this implode with blade. – weekapaug Oct 17 '17 at 18:44
  • ```{{ implode(', ', $colors) }}``` should work fine. Alternatively just loop over the colors using a foreach as well, so ```@foreach ($colors as $color) {{ $color }} @endforeach``` – Leon Vismer Oct 17 '17 at 18:59
  • laravel blade DOES recognize standard PHP btw. work just fine – ATechGuy Oct 17 '17 at 20:46
  • Thank you Leon, that was a great code example and exactly what I needed! – weekapaug Oct 18 '17 at 03:21