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:
我一直在与Laravel 4.1合作,创建一个有用户关系的图书列表应用程序。我有用户关系工作,但当我添加分页时,我得到以下错误异常:
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).
错误从视图(book/index.blade.php)生成,但错误异常实际上来自帮助程序(参见下面)。
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>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
书/ index.blade。php文件
@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]);
}
2 个解决方案
#1
21
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']
$book->image
is object notation, which can only be used to access object attributes and methods. try this:
$book->图像是对象表示法,只能用于访问对象属性和方法。试试这个:
@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.
这应该解决它。它的意思是,你以一种只能用于对象的方式调用一个非对象。如果有用,请告诉我。
#2
7
Try output gettype($book), if you get null in any point
如果您在任何点上都得到null,请尝试输出gettype($book)
{{$book['image']}} //will not complain about it but gives output ""
{{$book->image}} //will complain about it and throws an exception
I think that's the reason
我想这就是原因
I'm using laravel5 but I think it is the same with laravel4
我使用的是laravel5,但我认为它和laravel4是一样的。
#1
21
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']
$book->image
is object notation, which can only be used to access object attributes and methods. try this:
$book->图像是对象表示法,只能用于访问对象属性和方法。试试这个:
@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.
这应该解决它。它的意思是,你以一种只能用于对象的方式调用一个非对象。如果有用,请告诉我。
#2
7
Try output gettype($book), if you get null in any point
如果您在任何点上都得到null,请尝试输出gettype($book)
{{$book['image']}} //will not complain about it but gives output ""
{{$book->image}} //will complain about it and throws an exception
I think that's the reason
我想这就是原因
I'm using laravel5 but I think it is the same with laravel4
我使用的是laravel5,但我认为它和laravel4是一样的。