我正在尝试在应用程序中显示单个书籍。"books"表中的所有数据都可以显示,但没有其他数据。如果我想显示我所有的书,我也可以显示其他表中连接的数据,但如果我只想显示一本书的数据,我似乎无法让它发挥作用,也找不到问题。
图书管理员:
public function show($id)
{
$book = Book::findOrFail($id);
$languages = Language::all();
$genres = Genre::all();
$authors = Author::all();
$publishers = Publisher::all();
return view('books/show', compact('book', 'languages', 'genres','authors','publishers'));
}
show.blade.php:
@extends('layout')
@section('title')
<title>Alle Bücher</title>
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="uper">
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div><br />
@endif
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> {{ $book->title }}</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('books.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>ID:</strong>
{{ $book->id }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $book->title }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Autor(en):</strong>
@foreach($book->authors as $author)
{{$book->authors->name}}
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Jahr:</strong>
{{ $book->year }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Verlag(e):</strong>
@foreach($book->publishers as $publisher)
{{$publisher->name}}
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Genre(s):</strong>
@foreach($book->genres as $genre)
{{$genre->name}}
@endforeach
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Sprache:</strong>
{{$book->language->name}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>ISBN:</strong>
{{ $book->isbn }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Seitenzahl:</strong>
{{ $book->pages }}
</div>
</div>
</div>
@endsection
您有一个逻辑错误;{{ $book->authors->name }}
错误。您正确地循环$book->authors
,但随后引用了错误的变量。修正如下:
@foreach($book->authors as $author)
{{ $author->name }}
@endforeach
注释;稍后在代码中使用$book->publishers as $publisher
和$book->genres as $genre
可以正确地执行此操作,因此请检查一致性。