Saving related items when editing/creating a resource
Hi Everyone,
I'm trying to setup so that when I'm on the create/edit screen of my resource, I can add multiple items with a repeater and upon saving, those additional items are saved to their corresponding table.
ClientResource.php Form
Client Model
When the client resource creation runs, it creates the client item but doesn't create the contact(s).
13 Replies
Okay, so I found my issue but I now have a new one. My related items have a client_id on them but when I save or edit, it doesn't populate the needed field.
Is this in an admin panel, or standalone form package?
Admin panel. Sorry about that @Hugh Messenger
Hmm, then everything should happen automatically.
Is the client_id in $fillable on your Contact model?
Yes
This is the error I'm getting
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed
What's the rest of the error msg.
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: contact.client_id
Is that column fillable? Please share code
So it's not?!
Looks like you added 'contact_id' rather than 'client_id' in that $fillable. No reason for the Contact model to have a 'contact_id' attribute.
BTW, this kind of thing is a classic case where spending $5/month on Laravel Idea pays for itself. When adding attributes to things like $fillable on models, it provides autocompletion for the attribute names. If you only ever select from those rather than typing them yourself, it becomes impossible to make mistakes like this. And a single mistake like this can cost you days of productivity and wasted effort.
@Hugh Messenger Thanks for catching that. Late night programming at it's best. I've wanted to use Laravel Idea but it's only for PHPStorm which I don't own. Have a hard time paying yearly for an IDE when there's perfectly good ones that are free.
I can understand that. Although if you do this for a living, I'd see that as a false economy.