Reputation: 3
im really new to laravel so i apologize if i said something dumb but i want to make an accordion based on the created_at column of the boletines model. right now i have a semi working code that uses queries in the view but a friend of mine said that this is not a good practice. also it does not work when i try to add a filter as it gets broken when i add a whereYear() to the collection.
i want it to look like this enter image description here
this is what i have in the controller
public function boletinesAlertas()
{
$monthsArray = ['01' => 'enero', '02' => 'febrero', '03' => 'marzo', '04' => 'abril', '05' => 'mayo', '06' => 'junio', '07' => 'julio', '08' => 'agosto', '09' => 'septiembre', '10' => 'octubre', '11' => 'noviembre', '12' => 'dciembre'];
return view('private.boletinesAlertas', ['boletines' => new Boletines(), 'monthsArray' => $monthsArray]);
}
an in the view i have this frankencode
@foreach ($years = getDates($boletines->pluck('created_at'), 0, 4) as $year)
{{$year}}
@foreach($months= getDates($boletines->whereYear('created_at', $year)->pluck('created_at'), 5,2) as $month)
{{$monthsArray[$month]}}
@foreach($days = getDates($boletines->whereYear('created_at', $year)->whereMonth('created_at', $month)->pluck('created_at'),8,2) as $day)
{{$day}}
<table>
<tr>
<th>nombre</th>
</tr>
@foreach ($bolestines->whereDate('created_at', $year . '-' . $month . '-' . $day)->get() as $boletin)
<tr>
<td>{{$boletin->name}}</td>
</tr>
@endforeach
</table>
@endforeach
@endforeach
@endforeach
getDates is a helper function that i created that takes the created_at, and 2 values to get the month year or day by cropping the string.
is there a better way to do this? no only it looks bad when i try to add the filter function in the controller the whole thing stops working and i only shows the entries of the first month for some reason
Upvotes: 0
Views: 755
Reputation: 12835
What you are trying can be achieved via collection methods. And yes you should try to do all the data manipulation within controller methods.
Views should be responsible only for formatting and displaying data.
Try the below and see if that is what you want
public function boletinesAlertas()
{
$monthsArray = [
'01' => 'enero',
'02' => 'febrero',
'03' => 'marzo',
'04' => 'abril',
'05' => 'mayo',
'06' => 'junio',
'07' => 'julio',
'08' => 'agosto',
'09' => 'septiembre',
'10' => 'octubre',
'11' => 'noviembre',
'12' => 'dciembre'
];
$data = Boletines::get()->groupBy([
function($item){
return Carbon::parse($item->created_at)->format('Y');
},
function($item){
return Carbon::parse($item->created_at)->format('m');
},
function($item){
return Carbon::parse($item->created_at)->format('d');
}
]);
return view('private.boletinesAlertas', [
'data' => $data,
'monthsArray' => $monthsArray
]);
}
@foreach ($data as $year => $yearRows)
{{$year}}
@foreach($yearRows as $month => $monthRows)
{{$monthsArray[$month]}}
@foreach($monthRows as $day => $rows)
{{$day}}
<table>
<tr>
<th>nombre</th>
</tr>
@foreach ($rows as $boletin)
<tr>
<td>{{$boletin->name}}</td>
</tr>
@endforeach
</table>
@endforeach
@endforeach
@endforeach
Upvotes: 2