Laravel不返回视图上的数据->with()


Laravel not returning data on view->with()

我在return view()->with();语句返回数据时遇到了一点麻烦。

我得到的错误只是一个空白的屏幕,默认的laravel授权导航栏。

MySQL表结构:

id | version | active | slug

这是我的代码给你看。

function index() {
  $changelogs = Changelogs::where('active', '1')->orderBy('created_at', 'desc');
  return view('changelog.index')->with('changelogs', $changelogs);
}

Route (Incase you need)

Route::get('/release-notes', 'Changelogs'MyController@index');

<<p> 视图/strong>
@foreach($changelogs as $changelog)
<div class="row">
   div class="col-md-10 col-md-offset-1">
     div class="panel panel-default">
      <div class="panel-heading">
        <h3><a href="{{ url('/release-notes/'.$changelog->slug) }}">{{ $changelog->version }}</a></h3>
      </div>
      <div class="panel-body">
        {!! $changelog->body !!}
      </div>
    </div>
  </div>
</div>
@endforeach

tl:dr您需要添加->get()来完成您的流畅查询。

function index() {
  $changelogs = Changelogs::where('active', '1')->orderBy('created_at', 'desc')->get();
  return view('changelog.index')->with('changelogs', $changelogs);
}  

bonus您可以执行的所有方法都可以在API文档中找到。我最喜欢的一种方法是echo ->toSql(),这样您就可以检查SQL语句的完整性。

改变这一行:

$changelogs = Changelogs::where('active', '1')->orderBy('created_at', 'desc');

:

$changelogs = Changelogs::where('active', '1')->orderBy('created_at', 'desc')->get();