php Laravel 4 - 试图获取非对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20923931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:40:34  来源:igfitidea点击:

Laravel 4 - Trying to get property of non-object

phplaravellaravel-4

提问by Andrew

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) 生成,但错误异常实际上来自帮助程序(见下文)。

Helpers.php - Line 11 generates the error

Helpers.php - 第 11 行产生错误

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:

设置为强制地址为 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

书籍/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]);
}

回答by cppit

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->imageis object notation, which can only be used to access object attributes and methods. try this:

$book->image是对象表示法,只能用于访问对象的属性和方法。尝试这个:

 @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.

那应该可以解决它。它的意思是你正在以一种只能用于对象的方式调用非对象。让我知道它是否有效。

回答by PJ3

Try output gettype($book), if you get null in any point

尝试输出 gettype($book),如果你在任何时候得到 null

{{$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 相同