php 如何通过laravel中的外键检索记录的完整数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17873721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve full data of record by its foreign key in laravel?
提问by user2551866
I've been trying to figure out the proper way to get all the data from a record via a foreign key. I have simple app where users can add books to their "bookshelf".
我一直试图找出通过外键从记录中获取所有数据的正确方法。我有一个简单的应用程序,用户可以在其中将书籍添加到他们的“书架”中。
Here are my tables:
这是我的表:
USERS
用户
ID | NAME |
BOOKS
图书
ID | PAGES | NUMBER_OF_CHAPTERS
BOOKSHELF
书架
ID | USER_ID (foreign key to `users.id`) |
BOOKSHELF_BOOKS
书架_BOOKS
ID | BOOKSHELF_ID (foreign key to `bookshelf.id`) | BOOKS_ID (foreign key to `books.id`)
In my Eloquent Models, a bookshelf
hasMany books and bookshelf_books
belongsTo a bookshelf
and I have set those up in my models.
在我的Eloquent模型中, a bookshelf
hasMany books 和bookshelf_books
BeingsTo abookshelf
并且我已经在我的模型中设置了这些。
What happens is users create a "bookshelf" and add books to it from the list of books.
发生的事情是用户创建一个“书架”并从书籍列表中向其中添加书籍。
I'm at the point where I need to render the bookshelf with the user's books.
我现在需要用用户的书籍渲染书架。
When I retrieve my bookshelf books with a call like Bookshelf::find($id)->books, the books that belong to that bookshelf return just fine..but only columns from the bookshelf table. So I get in return the bookshelf id, the user id, and the book id.
当我使用 Bookshelf::find($id)->books 之类的调用检索我的书架书籍时,属于该书架的书籍返回得很好……但只有书架表中的列。所以我得到书架 id、用户 id 和书 id 的回报。
What I want to have returned is all the data of the book itself when i query for books in the bookshelf, not just it's id. E.G. [{"book_id":1, "pages":364, "number_of_chapters":14},{"book_id":2, "pages":211, "number_of_chapters":9}]
.
当我在书架中查询书籍时,我想要返回的是书籍本身的所有数据,而不仅仅是它的 id。EG [{"book_id":1, "pages":364, "number_of_chapters":14},{"book_id":2, "pages":211, "number_of_chapters":9}]
。
I've been scratching my head all day trying to figure out how to take this "join" one step further in Laravel/Eloquent ORM.
我整天都在挠头,试图弄清楚如何在 Laravel/Eloquent ORM 中更进一步地进行这种“加入”。
Would love any help with this, thanks!!
希望得到任何帮助,谢谢!!
回答by Laurence
Just eager load the relationship
Change
改变
Bookshelf::find($id)->books
to
到
Bookshelf::with('books')->find($id)->books
回答by Kylie
What about just doing it manually, and building your own array.
仅手动执行并构建自己的阵列怎么样。
$bookshelf = Bookshelf::find($id)->books;
$thebooks = array();
foreach($bookshelf as $val){
$book = Book::find($val->book_id);
$thebooks[] = $book;
}
Then just return $thebooks
to the View. Which will be an array of book models.
然后只需返回$thebooks
视图。这将是一系列书籍模型。
回答by user1669496
What you have should be correct.
你所拥有的应该是正确的。
$bookshelf = Bookshelf::find($id)->books;
$bookshelf = Bookshelf::find($id)->books;
To get your books, you would create a loop...
为了得到你的书,你会创建一个循环......
@foreach($bookshelf->books as $book)
{{ $book->name }}
{{ $book->title }}
@endforeach
To take this further, if you had an Author model which your books would belongTo()
, you could even do something like..
更进一步,如果你有一个 Author 模型,你的书belongTo()
会这样做,你甚至可以做类似的事情。
@foreach($bookshelf->books as $book)
{{ $book->name }}
{{ $book->title }}
{{ $book->author->name }}
@endforeach
回答by Raj Jagani
Try this query
试试这个查询
SELECT b.id, b.pages, b.number_of_chapters
FROM bookshelf bs
JOIN bookshelf_books bsb ON bsb.bookshelf_id = bs.id
JOIN books b ON b.id = bsb.books_id
WHERE bs.id = "Id of bookshelf"
回答by Vishnu Kant Maurya
SELECT *
FROM BOOKSHELF_BOOKS
JOIN BOOKSHELF ON BOOKSHELF_BOOKS.BOOKSHELF_ID = BOOKSHELF.ID
JOIN USERS.ID = BOOKSHELF.USER_ID
Try this sql statement, it will work.
试试这个 sql 语句,它会起作用。