Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Trying to get property of non-object

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:

ErrorException
Trying to get property of non-object (View: /app/views/books/index.blade.php)

return link_to_route('user.books.show', $book->title, [$book->user->username, $book->id]);

The error generates from the view (book/index.blade.php) but the error exception actually comes from the helper (see below).

Helpers.php - Line 11 generates the error

Controller - PARTIAL

public function show($id)
{
    $book = Book::findOrFail($id);

    return View::make('books.show', compact('book'));
}
}

My Route File

is set up to force the address to be the USERNAME>BOOKS>BOOK_ID:

#Books Controller

Route::resource('books', 'BooksController');

Route::get('books/{id}', 'BooksController@show')->where('id', '\d+');

//Books ID Rerouting  - USERNAME -> BOOK -> Book ID

Route::get('{username}/books', 'UserBooksController@index');

Route::get('{username}/books/{id}', ['as' => 'user.books.show', 'uses' => 'UserBooksController@show']);

Which is where I am getting the error - it no longer recognizes

user.books.show

books/index.blade.php file

    @foreach(array_chunk($books->getCollection()->all(), 3) as $row)    

    <div class="row">

    @foreach ($row as $book)

      <div class="col-md-4">
        <div class="thumbnail">
         <img data-src="{{ $book->image }}" alt="">
          <div class="caption">
            <h3>{{ link_to_book($book) }}</h3>
            <p>{{ $book->synopsis }} </p>
            <p><a href="{{ link_to_book($book) }}" class="btn btn-primary" role="button">Buy Now</a></p>
          </div>
        </div>
      </div>

    @endforeach
      </div>
    </div>
@endforeach     

{{ dd(Request::only('1')) }}    

{{ $books->appends(Request::only('1'))->links() }}  

HELPER FILE

<?php


function link_to_book(Book $book)

{
    return link_to_route('user.books.show', $book->title, [$book->user->username, $book->id]);
}
like image 207
Andrew Avatar asked Jan 04 '14 17:01

Andrew


Video Answer


1 Answers

I had the same issue and that's how I resolved it. To access the elements in the array, use array notation: $book['image']

$book->image is object notation, which can only be used to access object attributes and methods. try this:

 @foreach(array_chunk($books->getCollection()->all(), 3) as $row)    

    <div class="row">

    @foreach ($row as $book)

      <div class="col-md-4">
        <div class="thumbnail">
         <img data-src="{{ $book['image'] }}" alt="">
          <div class="caption">
            <h3>{{ link_to_book($book) }}</h3>
            <p>{{ $book['synopsis'] }} </p>
            <p><a href="{{ link_to_book($book) }}" class="btn btn-primary" role="button">Buy Now</a></p>
          </div>
        </div>
      </div>

    @endforeach
      </div>
    </div>
@endforeach     

{{ dd(Request::only('1')) }}    

{{ $books->appends(Request::only('1'))->links() }}  

That should resolve it. What its saying is that you are calling a non object in a way that should only be used for objects. Let me know if it works.

like image 88
cppit Avatar answered Sep 23 '22 07:09

cppit