0

I have a table inside my blade file and I'm trying to export it into a CSV.

Here is an example of my table that contains data from the Controller:

 @foreach($data['dailySummary'] as $date=>$day)
    <?php 
        $dateTime = DateTime::createFromFormat('Y-n-j', $date);
        $formattedDate = date('d M y', $dateTime->getTimestamp());
    ?>

    <tr>
        <td>{{ $formattedDate }}</td>
        <td>{{ ($day['day_totals'] != 0) ? $day['day_totals'] : '-' }}</td>

        <td>{{ ($day['5_0'] != 0) ? $day['5_0'] : '-' }}<!-- {{ $day['5_0'] }} --></td>   
        <td>{{ ($day['5_1'] != 0) ? $day['5_1'] : '-' }}</td>
        <td>{{ ($day['5_2'] != 0) ? $day['5_2'] : '-' }}</td>

        <td>{{ ($day['1_0'] != 0) ? $day['1_0'] : '-' }}</td>   
        <td>{{ ($day['1_1'] != 0) ? $day['1_1'] : '-' }}</td>
        <td>{{ ($day['1_2'] != 0) ? $day['1_2'] : '-' }}</td>

        <td>{{ ($day['4_0'] != 0) ? $day['4_0'] : '-' }}</td>    
        <td>{{ ($day['4_1'] != 0) ? $day['4_1'] : '-' }}</td>
        <td>{{ ($day['4_2'] != 0) ? $day['4_2'] : '-' }}</td>

        <td>{{ ($day['3_0'] != 0) ? $day['3_0'] : '-' }}</td>     
        <td>{{ ($day['3_1'] != 0) ? $day['3_1'] : '-' }}</td>
        <td>{{ ($day['3_2'] != 0) ? $day['3_2'] : '-' }}</td>

        <td>{{ ($day['2_0'] != 0) ? $day['2_0'] : '-' }}</td>     
        <td>{{ ($day['2_1'] != 0) ? $day['2_1'] : '-' }}</td>
        <td>{{ ($day['2_2'] != 0) ? $day['2_2'] : '-' }}</td>

        <td>{{ ($day['6_0'] != 0) ? $day['6_0'] : '-' }}</td>    
        <td>{{ ($day['6_1'] != 0) ? $day['6_1'] : '-' }}</td>
        <td>{{ ($day['6_2'] != 0) ? $day['6_2'] : '-' }}</td>

        <td>{{ ($day['day_total_price'] != 0) ? '$' . $day['day_total_price'] : '-' }}</td>
    </tr>

    @endforeach

The problem is that I'm not sure if I can even remove the html tags and include the data from within the blade brackets {{}}

This is just a segment of what I want to convert to CSV but I just want to see if there's another way for blade. I'm aware that there are hundreds or even thousands of html to csv tutorials online but not many have any to do with blade. Yes it may be the same as normal html, but after trying out the code I used to use for formatting into csv using a normal html page, it didn't work so well with blade.

remedy.
  • 2,032
  • 3
  • 25
  • 48

1 Answers1

0

This problem is not necessarily specific to Blade in Laravel since it can be done in PHP - already answered here.

I would personally prefer to have a method toCSV(), for example, in the model that you could request - it is a cleaner approach.

Community
  • 1
  • 1
cherrysoft
  • 1,165
  • 8
  • 17