title: Laravel 4 - Trying to get property of non-object
body: <p>I have been working with Laravel 4.1 to create a book list app with user relationships.  I have the user relationships working however when I added the pagination I get the following Error Exception:</p>
  
  <pre><code>ErrorException
  Trying to get property of non-object (View: /app/views/books/index.blade.php)
  
  return link_to_route('user.books.show', $book-&gt;title, [$book-&gt;user-&gt;username, $book-&gt;id]);
  </code></pre>
  
  <p>The error generates from the view (book/index.blade.php)  but the error exception actually comes from the helper (see below).</p>
  
  <p><img src="https://i.stack.imgur.com/ypiYl.jpg" alt="Helpers.php - Line 11 generates the error"></p>
  
  <p><strong>Controller - PARTIAL</strong></p>
  
  <pre><code>public function show($id)
  {
      $book = Book::findOrFail($id);
  
      return View::make('books.show', compact('book'));
  }
  }
  </code></pre>
  
  <p><strong>My Route File</strong> </p>
  
  <p>is set up to force the address to be the USERNAME>BOOKS>BOOK_ID:</p>
  
  <pre><code>#Books Controller
  
  Route::resource('books', 'BooksController');
  
  Route::get('books/{id}', 'BooksController@show')-&gt;where('id', '\d+');
  
  //Books ID Rerouting  - USERNAME -&gt; BOOK -&gt; Book ID
  
  Route::get('{username}/books', 'UserBooksController@index');
  
  Route::get('{username}/books/{id}', ['as' =&gt; 'user.books.show', 'uses' =&gt; 'UserBooksController@show']);
  </code></pre>
  
  <p>Which is where I am getting the error - it no longer recognizes </p>
  
  <pre><code>user.books.show
  </code></pre>
  
  <p><strong>books/index.blade.php file</strong></p>
  
  <pre><code>    @foreach(array_chunk($books-&gt;getCollection()-&gt;all(), 3) as $row)    
  
      &lt;div class="row"&gt;
  
      @foreach ($row as $book)
  
        &lt;div class="col-md-4"&gt;
          &lt;div class="thumbnail"&gt;
           &lt;img data-src="{{ $book-&gt;image }}" alt=""&gt;
            &lt;div class="caption"&gt;
              &lt;h3&gt;{{ link_to_book($book) }}&lt;/h3&gt;
              &lt;p&gt;{{ $book-&gt;synopsis }} &lt;/p&gt;
              &lt;p&gt;&lt;a href="{{ link_to_book($book) }}" class="btn btn-primary" role="button"&gt;Buy Now&lt;/a&gt;&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
  
      @endforeach
        &lt;/div&gt;
      &lt;/div&gt;
  @endforeach     
  
  {{ dd(Request::only('1')) }}    
  
  {{ $books-&gt;appends(Request::only('1'))-&gt;links() }}  
  </code></pre>
  
  <p><strong>HELPER FILE</strong></p>
  
  <pre><code>&lt;?php
  
  
  function link_to_book(Book $book)
  
  {
      return link_to_route('user.books.show', $book-&gt;title, [$book-&gt;user-&gt;username, $book-&gt;id]);
  }
  </code></pre>