【问题标题】:many to many retreiving book & author name error laravel多对多检索书籍和作者姓名错误 laravel
【发布时间】:2020-06-07 06:28:16
【问题描述】:

无法将所有书名和作者姓名显示为列表。得到错误。如果我在控制器(索引方法)中返回书籍和作者变量,我将数据作为数组。但无法在视图中访问它们。同样如下面的注释代码(在 Book Controller 中)所述,来自 laravel 文档的代码显示错误。

图书模型

class Book extends Model
{
 protected $guarded = [];

 public function authors(){
    return $this->belongsToMany('App\Models\Author')->withTimestamps();
 }
}

作者模型

class Author extends Model
{
 public $guarded = [];

 public function books(){
    return $this->belongsToMany('App\Models\Book')->withTimestamps();
 }
}

图书控制器

 public function index()
{
    $query = Book::with('authors')->get();
    Pls Read the commented lines below -
    //foreach ($query as $book) {
    //return $book->authors->author_name;  //Error: Property [author_name] does not exist on this collection instance.
    //}
    // (above code is given in laravel documentation but shows error : why?) 
    foreach($query as $b){
     $books[] = $b->book_name; 
     $authors[] = $b->authors->first()->author_name; 
    }
    return view('books.index', compact('books','authors'));
}

public function create()
{
    return view('books.create');
}

public function store(validateAuthorBook $request)
{
    $validated = $request->validated();
    if($validated){
        $book = new Book();
        $book->book_name = $request->book_name;
        $book->save();

        $author = new Author();
        $author->author_name = $request->author_name;
        $author->save();

        $book->authors()->attach($author);
    }

    return back()->with('status', 'Insert Successful!');
}

查看(index.blade.php)

  @foreach($books as $b)
    <tr>
        <th>{{$loop->iteration}}</th>
        <td>{{$b->book_name}}</td>
        //<td>how to get author name</td>
        @endforeach
    </tr>

错误

Facade\Ignition\Exceptions\ViewException
Trying to get property 'book_name' of non-object (View: 
D:\ProgrammingSSD\laragon\www\ulclibrary\resources\views\books\index.blade.php)

【问题讨论】:

    标签: laravel many-to-many


    【解决方案1】:

    您收到错误是因为您将 $book 设为数组 ($books[] = $b-&gt;book_name;) 中的字符串。你不应该对控制器中的$books 集合做任何事情,并且可以将它直接发送到视图。

    控制器:

    public function index()
    {
        $books = Book::with('authors')->get();
        return view('books.index', compact('books'));
    }
    

    查看(一行,用逗号分隔作者):

    @foreach($books as $book)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>{{ $book->book_name }}</td>
            <td>{{ $book->authors->pluck('author_name')->implode(', ') }}</td>
        </tr>
    @endforeach
    

    查看(循环,用换行符分割作者):

    @foreach($books as $book)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>{{ $book->book_name }}</td>
            <td>
                @foreach($book->authors as $author)
                    {{ $author->author_name }}<br/>
                @endforeach
            </td>
        </tr>
    @endforeach
    

    【讨论】:

    • 谢谢。第一个视图方法有效,但第二个显示错误 - 此集合实例上不存在属性 [作者]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多