vivek kumar
vivek kumar

Reputation: 55

how to save data in word file from database in laravel

I want to create a word file as a report. I want to fetch all data from the table and put to the word file but I am not able to save data to the file. I am doing in laravel 5.2. File is getting created and downloaded but data which is coming from database is not getting saved. thanks everyone Here is my controller code -

public function downloadAbleFile(Request $request){
    //Fetching all records based on projectid
    $project_id = $request->input('project_id');
    $questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();

    $headers = array(
        "Content-type"=>"text/html",
        "Content-Disposition"=>"attachment;Filename=report.doc"
    );

    $content = '<html>
                <head>
                <meta charset="utf-8">
                </head>
                <body>
                    <p>
                        <table>
                        <thead>
                        <tr>
                        <th>Project ID</th>
                        <th>Question Number</th>
                        <th>User ID</th>
                        <th>Answer</th>
                        <th>Status</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                            function project(){
                            foreach ($questiondetails as $question){
                                return(
                                    echo "<tr>";
                                    echo "<td>".$question->project_id."</td>";
                                    echo "<td>".$question->question_num."</td>";
                                    echo "<td>".$question->user_id."</td>";
                                    echo "<td>".$question->answer."</td>";
                                    echo "<td>".$question->status."</td>";
                                    echo "</tr>";
                                );
                            }
                        }
                        project();
                        ?>
                        </tbody>
                        </table>
                    </p>
                </body>
                </html>';

    return Response::make($content,200, $headers);
}

Upvotes: 1

Views: 1108

Answers (1)

Manuel Temple
Manuel Temple

Reputation: 763

Why do not you use blade? Try this

in your controller:

public function downloadAbleFile(Request $request){
    $project_id      = $request->input('project_id');
    $questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();

    $headers = array(
        "Content-type"        => "text/html",
        "Content-Disposition" => "attachment;Filename=report.doc"
    );

    $content =  View::make('path.view', [
                    'questiondetails' => $questiondetails,
                ])->render();

    return Response::make($content,200, $headers);
}

in your view

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <br>
        <table>
            <thead>
                <tr>
                    <th>Project ID</th>
                    <th>Question Number</th>
                    <th>User ID</th>
                    <th>Answer</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach($questiondetails as $question)
                <tr>
                    <td>{{ $question->project_id }}</td>
                    <td>{{ $question->question_num }}</td>
                    <td>{{ $question->user_id }}</td>
                    <td>{{ $question->answer }}</td>
                    <td>{{ $question->status }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
        <br>
    </body>
</html>

Upvotes: 1

Related Questions