在laravel中属于和有很多关系

时间:2019-10-06 19:20:50

标签: laravel eloquent

我有三本桌子书,类别和作者,即使它们之间建立了关系,我也只得到ID而不是Name。 我看到了很多链接,但没有弄清楚我的代码出了什么问题

我的图书模型

class books extends Model
{
    //
    protected $table='books';
    protected $primarykey = 'bookId';

    public function books_categories(){
        return $this->belongsTo('app\books_categories', 'catId', 'catId');
    }

    public function authors(){
        return $this->belongsTo('app\authors','authId','authId');
    }
}

我的作者模型

class authors extends Model
{
    //
    public function books(){
        return $this->hasMany('app\books','authId','authId');
    }
}

我的books_categories模型

class books_categories extends Model
{
    //
    public function books(){
        return $this->hasMany('app\books');
    }
}

我的书桌

    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('bookId');
            $table->string('bookTitle', 255)->nullable('false');
            $table->unsignedInteger('edition');
            $table->unsignedInteger('authId');
            $table->unsignedInteger('catId');
            $table->foreign('catId')->references('catId')->on('books_categories');
            $table->foreign('authId')->references('authId')->on('authors');
            $table->unsignedInteger('totalAvail')->default(0);
            $table->unsignedInteger('totalIss')->default(0);
            $table->timestamps();
        });
    }

我的控制器

    public function index()
    {
        $books = books::all();
        return view('booksList', ['books'=> $books]);

    }

我的观点

<div class="panel panel-default">
    <div class="panel-heading">Books List</div>
    <div class="panel-body">
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <th scope="col">BookID</th>
                <th scope="col">Title</th>
                <th scope="col">Edition</th>
                <th scope="col">Category</th>
                <th scope="col">Author</th>
                <th scope="col">Books Available</th>
                <th scope="col">Action</th>

            </tr>
            </thead>
            <tbody>

            @foreach($books as $book)
                <tr>
                    <td>{{$book->bookId}}</td>
                    <td>{{$book->bookTitle}}</td>
                    <td>{{$book->edition}}</td>
                    <td>{{$book->catId}}</td>
                    <td>{{$book->authId}}</td>
                    <td>{{$book->totalAvail}}</td>
                    <td>
                        <span><button type="button" class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-edit"></i></button></span>
                        <span><button type="button" class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-trash"></i></button></span>

                    </td>
                </tr>
        @endforeach
            </tbody>
        </table>
    </div>
</div>

enter image description here 如何获取姓名而不是ID

2 个答案:

答案 0 :(得分:3)

显示该ID是因为您通过写<td>{{$book->authId}}</td>

来告诉它

将其更改为

<td>{{$book->authors->name}}</td>

更改name以匹配您在数据库中拥有的属性名称。

有关如何处理口才关系的更多见解,请参阅此处的官方文档

  

https://laravel.com/docs/6.x/eloquent-relationships#one-to-many-inverse

答案 1 :(得分:0)

我发现实际上我的代码中有两个错误 1.我改变了看法  <td>{{$book->authId}}</td>  对此  <td>{{$book->authors->name}}</td> 在我的图书模型中,我更改了

    public function authors(){
        return $this->belongsTo('app\authors','authId','authId');
    }

对此

    public function authors(){
        return $this->belongsTo(authors::class,'authId','authId');
    }