Мой код продолжает выдавать ошибку при развертывании в облаке Google, но это работает локально
Попытка получить свойство 'image_url' не-объекта (View: / 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();
});
}
Я не знаю, не работает ли это при развертывании в облаке Google, но работает локально. Кажется, я не могу понять, что я делаю неправильно.
Я также попробовал эту схему
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();
});