首页 > 解决方案 > 试图获取非对象的属性“image_url”(查看:/app/resources/views/library.blade.php)

问题描述

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

试图获取非对象的属性“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();    
});

标签: laravel

解决方案


查询时,您可以使用 with 方法指定应该预先加载哪些关系: Blockquote

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

您可以使用Null Coalescing Operator来检查链式语句中的null 吗?(在 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

推荐阅读