belongsToMany column cannot be null
Hi
I have a database, which is defined by external sources, so structure cannot be amended.
I have a
users
table, which links to a reply
table using username
field on both tables. Then on the reply
table is a value
table, which links to groups.name
I have setup the relations, and doing a get on users and groups with the relation shows the related results.
Now, I have a RelationManager for Groups on the User resource, but when I try to attach groups, I just get an error that "Column 'value' cannot be null".
Has anyone come across this in the past, and if so, know how I can solve this. I suspect it's because of the non-standard column names, but I've got the relationships working fine.12 Replies
You probably need to set the primary keys for each model if they are different, but also doing that in the relaitonship too.
the cannot be null, means a value is being passed that is null that cannot be. So if it's an empty column and you want it to be empty, you will need to ensure on saving you cast it to empty opposed to null.
So, that's something that makes sense. Thought in the case of this DB, the primary keys are not the ones that go in the pivot table.
it's not straight forward, but this is a DB structure another bit of software uses, so I'm trying to get Laravel to be able to manage the data within the DB.
If it helps at all, this is shortened diagram showing the columns that are relevent to this
So its not performance but you can make the username match across. then the groups for the keys.
value is saying it cannot be empty which is true. So the value then maps to the groups.
It looks to me your value on the radreply relationship needs to be set as the group.name. That would solve your issue then
Yeah, I'm using a BelongsToMany relationship (both ways as it's Many to Many)
and the relationship works, like if I do a RadCheck::with('groups')->first()
I see the groups
and likewise
Group::with('radcheck')->first()
I see the checks that are related
I mean, if there's a simpler way to do it without the groups table - I don't mind using an alternative method
It should just be the way of saving data to the relationship as standard:
https://filamentphp.com/docs/3.x/panels/resources/relation-managers#creating-with-pivot-attributes
yeah, I got the relation manager setup
and I can see related groups fine, it's just when I attach i get the avove error, and also when I detatch, it deos nothing.
and you set the key as value -> name for the groups ?
on groups "id" column is the primary key, though I do want it to select name, so maybe I change that
There you go, it's maping across as ID but everything on the modle is using name to link
OK, just removed the ID column, and made name primary, now getting an error that it can't sort by
groups.id
Sort by name instead
Oh, is that a default attribute on the model?
I'm not specifying any sort myself
OK, so I'm back to where I was before getting value cannot be null
but with just name on the groups column
groups table*
Well, this is odd, I've just done this
and that attaches the model fine, but if I change the argument passed to attach() to $g, it gets the value cannot be null again
or if I change
'Test 2'
to $g->name
it attaches (doesn't add other pivot values to the pivot table I want, but that's another issue