1

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);
}

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.