首页 > 解决方案 > 未定义的属性:Illuminate\Pagination\LengthAwarePaginator::$id。它说问题出在我的索引中

问题描述

您好,我对 Paginator 有疑问,不知道如何使它工作。

这是我的报告控制器:

public function index()
    {
        //Show all created reports
        $reports = Reports::orderBy('created_at', 'desc')->paginate(15);
        return view('reports.index', ['reports'=>compact('reports')])->with(['reports', $reports]);
    }

这是 index.blade.php:

@foreach($reports  as $report)
         <tr>
             <td scope="row"><a href="/reports/show">{{$report->id}}</td>
             <td>{{$report->title}}</td>
             <td>{{$report->category}}</td>
             <td>{{$report->name}}</td>
             <td>{{$report->created_at}}</td>
             <td>{{$report->updated_at}}</td>
         </tr>
@endforeach 

标签: phplaravellaravel-7

解决方案


return view('reports.index', ['reports' => $reports]);
// or
return view('reports.index', compact('reports'));

你有的是这样的:

['reports' => ['reports' => $reports]]

如果您要使用with它将是:

...->with('reports', $reports);
// or
...->with(['reports' => $reports]);

推荐阅读