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
Repeater::make('contacts')
->relationship()
->schema([])
Repeater::make('contacts')
->relationship()
->schema([])
Client Model
public function contacts() {
return $this->hasMany(Contact::class);
}
public function contacts() {
return $this->hasMany(Contact::class);
}
When the client resource creation runs, it creates the client item but doesn't create the contact(s).
13 Replies
ThatWebGuy
ThatWebGuyOP16mo ago
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.
cheesegrits
cheesegrits16mo ago
Is this in an admin panel, or standalone form package?
ThatWebGuy
ThatWebGuyOP16mo ago
Admin panel. Sorry about that @Hugh Messenger
cheesegrits
cheesegrits16mo ago
Hmm, then everything should happen automatically. Is the client_id in $fillable on your Contact model?
ThatWebGuy
ThatWebGuyOP16mo ago
Yes This is the error I'm getting SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed
cheesegrits
cheesegrits16mo ago
What's the rest of the error msg.
ThatWebGuy
ThatWebGuyOP16mo ago
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: contact.client_id
Dennis Koch
Dennis Koch16mo ago
Is that column fillable? Please share code
ThatWebGuy
ThatWebGuyOP16mo ago
protected $fillable = ['name', 'contact_id', 'email', 'phone', 'fax', 'type'];
protected $fillable = ['name', 'contact_id', 'email', 'phone', 'fax', 'type'];
Dennis Koch
Dennis Koch16mo ago
So it's not?!
cheesegrits
cheesegrits16mo ago
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.
ThatWebGuy
ThatWebGuyOP16mo ago
@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.
cheesegrits
cheesegrits16mo ago
I can understand that. Although if you do this for a living, I'd see that as a false economy.
Want results from more Discord servers?
Add your server