Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

If you look above posts i posted a link to the books table and you will see it as id 63 & 65 then in authors the author of bates as ID of 6, so the only way to make this relationship is the name.

arabsight's avatar

the id in the books table in for the book not the author, you can rename the author field to author_id and make it an integer and update it with the ids of the authors from the authors table. if not you can update the values of the author field to match the last_name field from the author table.

theUnforgiven's avatar

I can't rename anything as this table comes from a CSV file the client uploads and they have specified it need to stay in this format.

bashy's avatar

No need to change anything major, just change how the relation works. It's really bad to use a string for it.

theUnforgiven's avatar

I've figured it out the authors table had "Bates" then books table had "bates" no caps B so after fixing that this works kinda now.

theUnforgiven's avatar

so bates has two books but using $book->books->first()->title only shows one result, what else can I use? to return all books (2) for this author? Just use $book->books->title ??

pmall's avatar
foreach($book->books as $b){
  echo $b->title;
}
2 likes
Previous

Please or to participate in this conversation.