Saving Optional Relationship to db does not persist
Edit: This is not solved, only halfway there :(
Hey all,
I've been trying my hand at Laravel and filament for the last couple of weeks and am stuck on something:
I have two entities, Book and BookPoster. Both have their foreign_key value set as nullable (because a book might not have a poster at creation).
The BookResource's form looks as follows:
When selecting a BookPoster from the list, the value does not get persisted in database. Do you know what I might be doing wrong?
Solution:Jump to solution
Why do you have a
book_id
on the book_poster
table? Can a poster only post one book?
Honestly I don't know how this would work with laravel or fillament by default. You essentially have 2 different relations here - A poster
had a book
and a book
has a poster
but they can be different.
I think you will need to override the default create method if you want to change the book_id
on the book_poster
table (or use a mutateData...
method)...18 Replies
ok, turns out I just needed to set the 'book_poster_id' as fillable in the Book entity; am dumb
But there's still half the issue; marking the property as fillable sets the id correctly on the book, but not on the book_poster. That value stays null, which means I cannot get the related data from the book instance
does not work in create or update
but the association shows up fine on one side
Problem comes when trying to access a property on the other element of the relation (i.e poster.path)
Does both the entities (model) have their relations defined?
is it a one to one relation? So that 1 book has 1 poster and vice-versa?
Book HasOne poster and poster BelongsTo a book
both have their relationship defined in the model
hows the mapping of the foreign keys? Do you have a book_id on the poster table?
book poster
book
If I update the book_posters table manually, everything shows up correctly on the table
if this is a one to one relation, only the belongsto table needs the foreign key (in this case, the book_id on the book_poster table). The problem must be on the poster() method on the book Model that it map the relation
Thats my guess
this is on the BookPoster:
this is on the Book:
but then, let's say I make the select like this:
I get the following error
I think the poster relation is trying to get the key from a column that doesnt exist...
for comparison, this at least populates the value correctly
and fills the book_poster_id column on the Book model (but not the other 😦 )
but that is on the Bookresource or Bookposterresource?
the foreignId on the BookPoster is generated like this:
book
can you try this on the Poster Model
no changes; making the select target the "poster" relation still throws the same error
do you have a repo so that I can check the code?
https://github.com/matfire/kz
back folder
upping this thread. I tried removing the relationship from the parent, but the
book_id
property is still not set on the BookPoster and, as such, the table view cannot retrieve those values to be displayed.
Anyone had to deal with this ? The relationship is optional, so that might pose an extra challenge 😦Solution
Why do you have a
book_id
on the book_poster
table? Can a poster only post one book?
Honestly I don't know how this would work with laravel or fillament by default. You essentially have 2 different relations here - A poster
had a book
and a book
has a poster
but they can be different.
I think you will need to override the default create method if you want to change the book_id
on the book_poster
table (or use a mutateData...
method)yes, a poster can only belong to a single book at a time, and a book can only have a single poster at a time; they unfortunately need to be separate table (client database spec 😦 )
ok, I managed to handle the create part, I think;
now I get the following error when going to the edit page:
Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::getOwnerKeyName()
ok, that one fixed itself when instead of referencing the hasOne property 'poster' I instead reference the book_poster_id
value
and this also selects the right option in the select field. I probably just need to do the same thing I did in the create part
and that worked! Hopefully it doesn't break xD
small detail: I had to specifically change the book_poster_id value on the Book model, otherwise it didn't update properly
thanks for the tip, Chester!!