swagster
swagster

Reputation: 165

Laravel not returning data on view->with()

I'm having a little bit of trouble returning data on a return view()->with(); statement.

The error I'm getting is just a blank screen with the default laravel auth navigation bar.

The MySQL table structure:

id | version | active | slug

Here is my code for you to look at.

Function

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

Route (Incase you need it)

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

View

@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

Upvotes: 0

Views: 66

Answers (2)

darronz
darronz

Reputation: 903

tl:dr You need to add ->get() to complete your fluent query.

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

bonus all of the methods you can perform can be found in the API docs. One of my favourites is to echo ->toSql() so you can sanity check the SQL statement.

Upvotes: 2

Osama Sayed
Osama Sayed

Reputation: 2023

Change this line:

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

To:

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

Hope this helps.

Upvotes: 0

Related Questions