thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

belongsTo relationship is not saving the value #5135

Closed sajjadalis closed 3 years ago

sajjadalis commented 3 years ago

Version information

Description

I have 2 models. Job and Organization. Job belongsTo Organization and Organization hasMany jobs. This is the relationship.

When i create relationship inside Jobs BREAD. It works fine and i can see all organizations in Select Dropdown (while creating new job). I select one Organization and Submit the job. but it doesn't save the value inside database and organization_id is set to Null (when organization_id field is nullable). If field is not nullable. It gives this error. SQLSTATE[HY000]: General error: 1364 Field 'organization_id' doesn't have a default value

Similar issue with user_id and newspaper_id. column (same relationship with Job). I can see all users and newspapers listed in Select Dropdown but they're not saving to database. SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value

Job model have multiple belongsToMany relationships with other models and all of them are working fine. Only issue with belongsTo relationship.

screenshot

ss

ss

MrCrayon commented 3 years ago

First thing your organization relationship is not set correctly, reference column should be organization_id not id. If it's still not saving verify that organization_id field in BREAD settings has edit and add checked. Since it's saving the value in that field if edit and add are not checked whatever you send will be ignored.

Let us know if that solves the problem.

sajjadalis commented 3 years ago

Yes. It was add and edit issue for organization_id field in BREAD setting. I unchecked all options for organization_id (and for newspaper_id and user_id). Thanks for the help. It looks all good.

That relationship reference was a mistake before taking screenshot. I deleted relationship after making some changes to migration table and then added it back.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.