frankhosaka's avatar

I want show details inside a group

In Controller I have:

public function group(){
        $groups=tbpedido::groupBy('dia')->select('dia')->get();
        $details=tbpedido::get();
        return view('groups',compact('groups','details'));

In groups.blade.php I have:

<table><th>Dia<th>Pedido
@foreach($groups as $group)
<tr><td>{{$group->dia}}
@php 
$filtereds=$details->where('dia',$group->dia);
foreach($filtereds as $filtered):
    echo "<td><td>$filtered->ped";
endforeach
@endphp
@endforeach
<table>

Is there a ellegant way to make same task?

0 likes
10 replies
tykus's avatar

You could use the Collection keyBy method instead (if the overall number of records is not excessive). This will group the records by the given property and you save a query:

public function group()
{
    $groups=tbpedido::get()->keyBy('dia');
    return view('groups',compact('groups'));
}

I am not 100% sure of your HTML table structure but you need nested foreach statements to render the results:

@foreach($groups as $dia => $group)
<tr>
    <td>{{$dia}}</td>

     @foreach($group as $tbpedido):
     <td>{{ $tbpedido->ped }}<td>
    @endforeach
</tr>
@endforeach
1 like
frankhosaka's avatar

@tykus Collection aproach doesn't work, he got only first ped number for each dia.

tykus's avatar

@frankhosaka that should not be the case; each group should (assuming there are many records with similar dia values) have multiple items.

frankhosaka's avatar

Lary answer works, but the post did disappear! The answer was that:

Model

class tbpedido extends Model{
public function details() {return $this->hasMany(tbpedido::class,'dia','dia');}}

Controller

public function group(){
        $groups=tbpedido::groupBy('dia')->select('dia')->get();
        $details=tbpedido::with('details')->get();
        return view('groups',compact('groups','details'));    }
´´´
Blade
DiaPedido @foreach($groups as $group) {{$group->dia}} @foreach($group->details as $detail) {{$detail->ped}} @endforeach @endforeach ``` It was not original response, I addapted.
tykus's avatar

@frankhosaka unused variable and associated queries

$details=tbpedido::with('details')->get();

N+1 issue introduced

@foreach($group->details as $detail)

The relationship is a bad solution IMHO; there is no relationship really.

frankhosaka's avatar

Blade

<table><th>Dia<th>Pedido
@foreach($groups as $group)
<tr><td>{{$group->dia}}
    @foreach($group->details as $detail)
        <td>{{$detail->ped}}
    @endforeach
@endforeach
</table>
frankhosaka's avatar

I found a copy of Lary answer in my email:

One possible solution to make the task more elegant is to use Laravel's Eloquent relationships. You can define a relationship between the tbpedido model and itself based on the dia attribute, and then use the with method to eager load the related details for each group. Here's an example:

In the tbpedido model, define the relationship:

class tbpedido extends Model { public function details() { return $this->hasMany(tbpedido::class, 'dia', 'dia'); } } In the controller, use the with method to eager load the details:

public function group() { $groups = tbpedido::groupBy('dia')->select('dia')->get(); $details = tbpedido::with('details')->get(); return view('groups', compact('groups', 'details')); } In the view, you can then access the related details for each group using the details relationship:

@foreach($groups as $group) {{ $group->dia }} @foreach($group->details as $detail) {{ $detail->ped }} @endforeach @endforeach This way, you avoid having to manually filter the details for each group, and the code becomes more readable and maintainable.

Please or to participate in this conversation.