So you this is how I would plan this out
- User model, User has many books
- Book model, Book has many UserBook / Book has many Authors
- Author model, Author has many books
- UserBook, UserBook belongs to User / UserBook belongs to Book
So with the above in mind
- User model needs no foreign key columns
- Book model needs no foreign key columns
- Author model needs no foreign key columns
- UserBook is a model NOT a pivot. it represents a unique set of data which has foreign key to the related User (with
user_id) and related Book (withbook_id)
Then you need a pivot table for author_book to make the many to many relationship between authors and books, considering that authors can write many books and book can have more than one author.
Just thinking about getting access to the user's books
User.php
public function userbooks()
{
return $this->hasMany(UserBook::class);
{
UserBook.php
public function user()
{
return $this->belongsTo(User::class);
}
public function book()
{
return $this->belongsTo(Book::class);
}
Book.php
public function userbooks()
{
return $this->hasMany(UserBook::class);
}
public function authors()
{
return $this->belongsToMany(Author::class);
}
Author.php
public function books()
{
public function belongsToMany(Book::class);
}
When listing the user's books, I would go indirectly like
$userBooks = $user->userbooks()->with('book')->get();
and then you have access to the customizations in $userbooks and the original via $userbook->book->title;
If you don't want access to the customization you can also use the hasManyThrough relationship;
User.php
public function userbooks()
{
return $this->hasMany(UserBook::class);
{
public function books()
{
return $this->hasManyThrough(Book::class , UserBook::class);
}
Now you should be able to do
$books = $user->books()->get();
Hope this helps.
(I've not considered sources as you have not said too much about that, but my guess is that there would be a model+table for Source and a key on the book model so that it can belong to a source)