laravel - 试图获取非对象的属性“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();
});
解决方案
查询时,您可以使用 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
推荐阅读
- python-2.7 - ld 在arch linux上构建时找不到与python模块相关的函数
- java - ListView 以编程方式添加列
- spring-data-jpa - JPA 存储库本机查询忽略 ::timestamptz
- haskell - `[ (x !! 0, x !! 1) | x <- mapM (const ['A', 'B', 'C'] ) [1..2], head x < head (tail x) ]` 有效吗?
- python - 使用 Python ElementTree 和 minidom 漂亮地打印 XML 时禁用转义
- excel - 从链接地址复制工作簿并将其粘贴到范围地址
- angular - 对服务变量的更改不影响组件
- python - 如何将两个文本文件与 python 进行比较,如果某一行不同则触发输出?
- c - 全局结构体数组非常规问题
- algorithm - 修改给定的伪代码,使其不包含循环