简体   繁体   English

三个表之间的Laravel关系

[英]Laravel relationship between three tables

I have 3 tables: 我有3张桌子:

  • Events 活动
  • Ages 时代
  • Books 图书

Which I was relating them in the following way: 我通过以下方式关联它们:

  • ages 年龄

    • id ID
    • name 名称
  • books 图书

    • id ID
    • title 标题
  • age_book age_book

    • id ID
    • age_id age_id
    • book_id book_id
  • events 事件

    • id ID
    • name 名称
  • event_age EVENT_AGE

    • id ID
    • event_id EVENT_ID
    • age_id age_id
  • event_age_book event_age_book

    • event_age_id event_age_id
    • book_id book_id

Basically, a book can have many ages, an age can have many books, an event can have many ages, an event age can have many books. 基本上,一本书可以有很多年龄,一个年龄可以有很多本书,一个事件可以有很多年龄,一个事件可以有很多本书。

My Event Model looks like this and works fine to get all event ages 我的事件模型如下所示,可以很好地获得所有事件的年龄

class Event extends Model
{
    public function ages()
    {
        return $this->belongsToMany(Age::class, 'event_age');
    }
}

Age Model: 年龄模型:

class Age extends Model
{
    public function books(): BelongsToMany
    {
        return $this
            ->belongsToMany(Book::class)
            ->withTimestamps();
    }
}

Book Model 书本模型

class Book extends Model
{
    public function ages(): BelongsToMany
    {
         return $this
            ->belongsToMany(Age::class, 'book_age', 'book_id','age_id')
            ->withTimestamps();
    }
}

Im having trouble figuring out how to get all edition age books, is it possible to do it on the Event model? 我在弄清楚如何获取所有版本的年龄书籍时遇到了麻烦,是否可以在Event模型上进行操作?

Let's Review The Relationships 让我们回顾一下关系

Seems you should only need 3 models, and 5 tables. 似乎您只需要3个模型和5个表。

  • Books Belong to Ages 历代书籍
  • Events Belong to Ages 历代事件
  • Ages Belongs to Books 年龄属于书籍
  • Ages Belongs to Events 年龄属于事件

Your other models are alright, but your age model is missing a relationship to Event: 您的其他模型还不错,但是您的年龄模型缺少与Event的关系:

class Age extends Model
{
    public function books(): BelongsToMany
    {
        return $this->belongsToMany(Book::class)
            ->withTimestamps();
    }

    public function events(): BelongsToMany
    {
        return $this->belongsToMany(Events::class,'event_age')
            ->withTimestamps();
    }
}

That would allow: 这将允许:

$books->ages;
$events->ages;
$ages->book;
$ages->events;

And chaining... 和链接...

$books = collect();
foreach($event->ages as $age){
    $books->merge($ages->books);
}
$books->unique();

Books and Events 书籍和活动

So, I gon't think you want age_event_book s. 因此,我认为您不需要age_event_book I think what you really want is: 我认为您真正想要的是:

  • Events belong to Books 活动属于图书
  • Books belong to Events 书籍属于活动

such that 这样

book_events
- id
- book_id
- event_id
- age_id

And you'd have in book: 您将在书中:

public function events()
{
    return $this->BelongsToMany('App\Event')
       ->withTimestamps()
       ->withPivot('age_id');
}

And in event: 并且在情况下:

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

Giving you: 给你:

$event->books
$book->events

On the Front End 在前端

[O]n the frontend i'll need to get the most recent event and group books by age and books can belong to more than one age 在前端,我需要获取最新的事件,并按年龄分组书籍,并且书籍可以属于一个以上的年龄

$event = Event::latest();
$books = $event->books();

Then on the blade 然后放在刀片上

@foreach($books as $age_id => $books)
    <h4>{{Age::find($age_id)->name}}</h4>
    @foreach($books as $book)
       <div>$book->name</div>
    @endforeach
@endforeach

Helpful Tip 有用的提示

You are supplying the relation table, which you have to do because you didn't follow the naming convention for joining tables. 您正在提供关系表,您必须这样做,因为您没有遵循联接表的命名约定。 The convention is that the classes being joined should be listed alphabetically. 约定是要加入的类应按字母顺序列出。 So age_event instead of event_age . 因此,使用age_event代替event_age

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM