【问题标题】:Trying to get property 'image_url' of non-object (View: /app/resources/views/library.blade.php)试图获取非对象的属性“image_url”(查看:/app/resources/views/library.blade.php)
【发布时间】:2020-01-24 12:32:44
【问题描述】:

当我部署到谷歌云时,我的代码一直返回错误,但这在本地工作

试图获取非对象的属性“image_url”(查看:/app/resources/views/library.blade.php)

在我看来,我有

              @forelse($favorites as $fav)
                    <div class="col-6 col-md-3">
                      <a href="{{ route('book', $fav->id) }}">
                      <img src="{{ $fav->book->image_url }}" alt="trending image" />
                      </a>
                      <p class="authorone">{{ $fav->book->author->name }}</p>
                      <h1 class="book-title">{{ $fav->book->name }}</h1>
                      @empty
                      <h2>You have no favourites</h2>
                      <br/><br/>
                      <a href="{{ route('discover') }}" class="return-button">Return To Discover</a>
                  </div>
               @endforelse

这是我的控制器

public function mylibrary(){
   // $mybooks = MyBook::where('user_id', Auth::id())->get();
    $favorites = Favorite::where('user_id', Auth::id())->get();
    return view('library')->with('favorites', $favorites);

 }

在我的Favorite Model,我有这个

public function book ()
{
    return $this->belongsTo(Book::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

在我的Book Model,我有这个

public function favorites()
{
    return $this->hasMany(Favorite::class);
}

在我的User Model,我有这个

public function favorites(){
    return $this->hasMany(Favorite::class);
}

我最喜欢的表格

public function up()
{
    Schema::create('favorites', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('book_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->timestamps();
    });
}

我不知道当我部署到谷歌云时它不起作用,但它在本地工作。我似乎无法弄清楚我做错了什么。

我也试过了

public function mylibrary(Book $book){
   // $mybooks = MyBook::where('user_id', Auth::id())->get();
    $favorites = Favorite::where('user_id', Auth::id())->get();
    return view('library')->with('favorites', $favorites)->with('book', $book);

 }

我的书架构

Schema::create('books', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->text('about');
    $table->string('image');  
    $table->string('image_url');
    $table->string('epub_url');
    $table->integer('author_id'); 
    $table->string('publisher');  
    $table->year('year');
    $table->boolean('recommended')->default(0);
    $table->timestamps();    
});

【问题讨论】:

  • $fav-&gt;booknull,所以$fav-&gt;book-&gt;image_url 不起作用。执行dd($favourites) 并确认每个Favorite 都有一个关联的book
  • 它返回值。
  • 它返回值......那是......难以置信地没用大声笑。循环 $favourites 时发生错误,因此其中一个迭代 ($fav) 没有关联 book。在尝试访问 book-&gt;whatever 之前,您可以调试哪一个并添加一些代码来检查 book
  • 哦,是的,我发现有些没有价值。虽然有些有价值。
  • ^ 你去吧;因此,在循环时,检查 $fav-&gt;bookbook 还是 null 并相应地输出。

标签: laravel


【解决方案1】:

查询时,可以使用 with 方法指定哪些关系应该是eager loaded: 块引用

public function mylibrary() {
    $data = User::with('favorites', 'favorites.book')->find(Auth::id());
    return view('library')->with('userFavorites', $data);
}

您可以使用Null Coalescing Operator ??(在 PHP 7 中引入)检查链式语句中的空值:

//...
<img src="{{ $fav->book->image_url ?? '' }}" alt="trending image" />
//...

编辑

@forelse($userFavorites->favorites as $fav)
     <div class="col-6 col-md-3">
         <a href="{{ route('book', $fav->id) }}">
            <img src="{{ $fav->book->image_url ?? '' }}" alt="trending image"/> 
         </a>
         <p class="authorone">{{ $fav->book->author->name ?? '' }}</p>
         <h1 class="book-title">{{ $fav->book->name ?? '' }}</h1>
@empty
         <h2>You have no favourites</h2>
         <br/><br/>
         <a href="{{ route('discover') }}" class="return-button">Return To Discover</a>
     </div>
@endforelse

【讨论】:

  • 感谢它的工作,但它返回一个空框,因为它是空的。我怎样才能防止这种情况发生?
猜你喜欢
  • 2018-03-22
  • 2019-02-24
  • 2020-09-26
  • 2019-04-08
  • 2018-09-25
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多