调用未定义的关系

时间:2018-10-23 18:04:28

标签: php laravel

嗨,我有2个表,其中有很多对很多rel,我收到此错误消息:

我在XAMPP堆栈上使用Laravel 5.6。

  

调用模型[App \ Book]上未定义的关系[类别]。

任何人都可以告诉我问题出在哪里吗?

这是我的类别模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function Book (){
        return $this->hasMany(Book::class);
    }
}

这是我的Book模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    use Notifiable;
    public $timestamps = false;
    Protected $fillable =                 
    ['name','pages','price','description','ISBN','VIMG','published_at'];

    public function user(){
        return $this->belongsTo(user::class);
    }

    public function categories(){
        return $this->belongsToMany(Category::class);
    }

    public function authors(){
        return $this->belongsToMany(Author::class);
    }
}

这是我的刀片(show.blade.php):

<!
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0,         
maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        <h1>
            Name: {{$book->name}}
        </h1>
        <p>
            Pages: {{$book->pages}}
        </p>
        <p>
            ISBN: {{$book->ISBN}}
        </p>
        <p>
            Published_at: {{$book->published_at}}
        </p>
        <p>
            Price: {{$book->price}} (T)
        </p>
        <p>
            Creator: {{$book->user->name}}
        </p>
        Categories:
        @foreach($book->categories as $category)
        <em>
            {{$category->name . ','}}
        </em>
        @endforeach

        @foreach($book->authors as $author)
            <em>
                {{$author->name . ','}}
            </em>
        @endforeach
    </div>
</body>
</html>

坦克帮了我很多!!!!

0 个答案:

没有答案