Home > database >  How can I store foreign Keys as arrays in laravel?
How can I store foreign Keys as arrays in laravel?

Time:10-11

I have 3 models:

  • Book
  • Author
  • Owner

Book has:

  • id
  • name
  • photo
  • author_id
  • owner_id
  • is_available

Author has:

  • id
  • name
  • photo

Owner has:

  • id
  • name
  • photo
  • book_id

A book can have multiple Authors and Owners.

How can I store more single id in author_id and owner_id?

CodePudding user response:

If the author has many books, and the book have many authors, than it’s a many to many relationship and you will need intermediate tables author_book and owner_book. In order to be able to define many author ids for one a book and many books for one author. Check the Laravel doc here for implementation details https://laravel.com/docs/9.x/eloquent-relationships#many-to-many

Good luck.

  • Related