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
toeknee
toeknee2w ago
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.
sdousley
sdousleyOP2w ago
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
No description
toeknee
toeknee2w ago
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
sdousley
sdousleyOP2w ago
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
sdousley
sdousleyOP2w ago
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.
toeknee
toeknee2w ago
and you set the key as value -> name for the groups ?
sdousley
sdousleyOP2w ago
on groups "id" column is the primary key, though I do want it to select name, so maybe I change that
toeknee
toeknee2w ago
There you go, it's maping across as ID but everything on the modle is using name to link
sdousley
sdousleyOP2w ago
OK, just removed the ID column, and made name primary, now getting an error that it can't sort by groups.id
toeknee
toeknee2w ago
Sort by name instead
sdousley
sdousleyOP2w ago
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
Route::get('/', function (\Illuminate\Http\Request $request) {
$g = \App\Models\Radius\Group::where('name', '=', 'Test 2')
->first();
$c = \App\Models\Radius\Check::first();

$c->groups()->attach('Test 2');
dd($g, $c);
});
Route::get('/', function (\Illuminate\Http\Request $request) {
$g = \App\Models\Radius\Group::where('name', '=', 'Test 2')
->first();
$c = \App\Models\Radius\Check::first();

$c->groups()->attach('Test 2');
dd($g, $c);
});
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
Want results from more Discord servers?
Add your server