简体   繁体   English

与laravel中三个表的关系

[英]Relationship with three tables in laravel

I need help to get data from my database.我需要帮助才能从我的数据库中获取数据。 I have the following models:我有以下型号:

-User
--Id
--Name

-Book
--Id
--Name

-List
--Id
--Title

-BookList
--id
--List
--Book
--User

I am using eloquent and I would like do get data following the structure below:我正在使用 eloquent,我希望按照以下结构获取数据:

-User
--List 1
--- Book 1
--- Book 2
--List 2
--- Book 1
--- Book 3

In other words, a user with multiple lists of books.换句话说,用户拥有多个书籍列表。 In the User.php model I made the following relationship:在 User.php 模型中,我建立了以下关系:

public function lists ()
{
  return $ this-> belongsToMany (
    List :: class, // related
    'books_lists', // table
    'user', // foreign pivot key
    'list' // related pivot key
  );
}

public function books ()
{
   return $ this-> belongsToMany (
      Book :: class, // related
      'books_lists', // table
      'user', // foreign pivot key
      'book' // related pivot key
   );
}

This way I get the following return:这样我得到以下回报:

-User
--List 1
--List 2
--Book 1
--Book 3

What kind of relationship I need to do to get data in desired structure?我需要做什么样的关系才能获得所需结构的数据?

Thank you.谢谢你。

The best structure would be to merge the list and booklist table (since every book list can only have one title anyway)最好的结构是合并列表和书单表(因为无论如何每个书单只能有一个标题)

-Users
--id
--name

-books
--id
--name

-book_list
--book_id
--list_id

-lists
--id
--title
--user_id

In the User.php在 User.php 中

public function lists ()
{
  return $this->hasMany(List::class);
  //no need to define the foreign keys if you name the tables and fields like above
}

in the List.php在 List.php

public function books ()
{
  return $this->belongsToMany(Book::class);
  //no need to define the foreign keys if you name the tables and fields like above
}

now you can get the results with this现在你可以得到这个结果

$user = User::with('lists.books')->find($userId);

the result will be as follow结果如下

-user
--List 1
---Book 1
---Book 2
--List 2
---Book 1
---Book 4

A book can be added to multiple list, right?一本书可以添加到多个列表中,对吗? Your database structure can be better, but I assume your ERD is fixed by designer/leader.您的数据库结构可以更好,但我认为您的 ERD 是由设计师/领导者修复的。 So just do it my instruction:所以就按照我的指示去做:

Add this relation to User Model(User.php):将此关系添加到用户模型(User.php):

public function lists ()
{
  return $this->belongsToMany (
    List::class, // related
    'books_lists', // table
    'user', // foreign pivot key
    'list' // related pivot key
  );
}

And this relation to List Model(List.php):与列表模型(List.php)的这种关系:

public function books ()
{
   return $this->belongsToMany (
      Book::class, // related
      'books_lists', // table
      'list', // foreign pivot key
      'book' // related pivot key
   )->withPivot(["user"]);
}

to return返回

$user = User::with(['lists.books'=> function($q)use($id){
$q->wherePivot('user', '=', $id);}])->first();
dd($user->toArray())

But it's better to change your structure to:但最好将您的结构更改为:

-User
--Id
--Name

-Book
--Id
--Name

-List
--Id
--Title

-BookList
--id
--List
--Book

-UserList
--List
--User

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

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