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

faisal.arbain@gmail.com's avatar

ook. then just save as plain string or json i guess. you still can convert them back into array.

faisal.arbain@gmail.com's avatar

by the way.. the error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.authors_id' in 'where clause' (SQL: select * from `books` where `books`.`authors_id` = 2) 

was triggered by $author->books->first() as it expect books to keeps reference to author instead of author having book_id

theUnforgiven's avatar

That's my point all am wanting to do is show the books relating to the author but because there's no ID (as I previously stated) there's no way to do this, is there?

faisal.arbain@gmail.com's avatar

try this. not sure whether this is you looking for. this should resolve your error. but the relationship is one to one

class Authors extends \Eloquent{

    public function books()
    {
        return $this->belongsTo('Books');
    }

}


class Books extends \Eloquent{

    public function author()
    {
        return $this->hasOne('Authors');
    }
}
<td>{{ $author->books->title }}</td>
faisal.arbain@gmail.com's avatar

do you mean you want to get the book from the author's name. $author->books that retrieve books by the author_name stored in books table?

faisal.arbain@gmail.com's avatar
class Authors extends Eloquent{
    function books(){
        return $this->hasMany('Books', "author","name");
    }
} 
theUnforgiven's avatar

Can you explain what this is doing please and then what I should in the view?

bashy's avatar

It's custom fields in the pivot table.

faisal.arbain@gmail.com's avatar

when you call author->books is will find all books with books.author equal to the author name. (that what i understand from your explanation). since the books don't have any key to the author except they have same books.author and author.name, i thought that the only way you can get the books thats belongs to the author.

i hope i get your right this time. or maybe you should start over to explain your issue. this is far from exploding category already. huhu...

theUnforgiven's avatar

Yes I did a pretty shitty job explaining myself I think, sorry about that! Let me try this as you say then report back shortly.

theUnforgiven's avatar

maybe so, but i think i didn't explain myself correctly. unless @bashy can see what I mean

bashy's avatar

Never dealt with CSVs, sorry. I can't really see from 3 pages either :P

Previous

Please or to participate in this conversation.