如何从laravel5.5获取数据?

时间:2017-10-18 05:02:08

标签: laravel

我正在使用Laravel 5.5,我想从我的视图页面(home.blade.php)查看数据库中的数据



 <table>
                        <tbody>
                    @foreach($home as $key => $data)
                        <tr>
                            <th>{{$data->created_at}}</th>
                            <th>{{$data->category}}</th>
                            <th>{{$data->reportitle}}</th>
                            <th>{{$data->reportdetails}}</th>
                            <th>{{$data->seenstat}}</th>
                            <th>{{$data->actionstat}}</th>
                            <th>{{$data->donestat}}</th>
                        </tr>
                    @endforeach
                        </tbody>
                    </table>
&#13;
&#13;
&#13;

在我的home.blade.php中我有一个空表,我想显示来自数据库fyp的数据:

&#13;
&#13;
 Route::get('home', function () {

        $fyp = DB::table('reports')->get();

        return view('home', ['fyp' => $fyp]);

    });
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

这必须奏效(正如@Nazmul所说):

@foreach($fyp as $data)
    <tr>
        <th>{{$data->created_at}}</th>
        <th>{{$data->category}}</th>
        <th>{{$data->reportitle}}</th>
        <th>{{$data->reportdetails}}</th>
        <th>{{$data->seenstat}}</th>
        <th>{{$data->actionstat}}</th>
        <th>{{$data->donestat}}</th>
    </tr>
@endforeach

答案 1 :(得分:0)

只需按照代码

Route::get('home', function () {

            $home = DB::table('reports')->get();

            return view('home', compact('home'));

});

在您的视图页面

之后
<table>
                        <tbody>
                    @foreach($home as $key => $data)
                        <tr>
                            <th>{{$data->created_at}}</th>
                            <th>{{$data->category}}</th>
                            <th>{{$data->reportitle}}</th>
                            <th>{{$data->reportdetails}}</th>
                            <th>{{$data->seenstat}}</th>
                            <th>{{$data->actionstat}}</th>
                            <th>{{$data->donestat}}</th>
                        </tr>
                    @endforeach
                        </tbody>
                    </table>

我希望这会对你有所帮助。