try this
Schema::table('users', function($table)
{
$table->string('name', 50)->nullable()->change();
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, Is it possible to make a foreignId column in a migration file in laravel 7 as a nullable column? unfortunately it does not work for me.
thanks for any hint,
try this
Schema::table('users', function($table)
{
$table->string('name', 50)->nullable()->change();
});
thanks to your reply.
Is "name" column defined as a foreign key?
@zizi_ove no its making column nullable use this
Firstly you have to make your user_id field an index:
$table->index('user_id');
After that you can create a foreign key with an action on cascade:
$table->foreign('user_id')->references('id')->on('users');
@jeevamugunthan could you please describe a little more, why I should index a foreign key column? indexing is necessary? Does it help to make a "foreign key column" nullable?
Thank you
Hi all, fortunately I found the solution. for making a foreign key column nullable in laravel 7, just write "nullable()" before "constrained()" and it works! like this:
$table->foreignId('type_id')->nullable()->constrained()->onUpdate('cascade')->onDelete('set null');
Good luck.
Just a note. Making a foreign key nullable defeats the purpose of it.
Depending a bit on where the foreign key is placed it serves different purposes.
The table with the foreign key needs to have a reference while that is not true to the table it references.
Let's say we have a users table and in it we have a profile_id that means that each user must have a profile, but if we put the user_id in the profiles table then the user doesn't need a profile but the profile needs a user.
@tray2 I've had similar discussions on here in the past. There are loads of circumstances where you need a nullable fk
Take a parent child relationship for instance. User record might need parent_id column to point to another User. But not all User can be children, so some will have a null parent.
@snapey that is true however in that case I'd might consider using a pivot table for it instead.
In my case, I have "user" table and in it, there is "gender_id" column. There is also another table named "gender" which has :
id=1 ==> name=woman, id=2 ==> name=man
so in "user" table, "gender_id" column refers to "gender" table (FK). but I made "gender_id" nullable because the gender field in the registration form is optional.
thank you all for your help.
In your case I'd just add another record to the gender table
id = 3 ==> name=unspecified
And that way keeping the database integrity. And you can use the 3 as a default value if null is given.
wow, a whole table just for 1 or 2
why not just store M or F in the User
@tray2 Thank you very much for your advice.
@snapey I thought it is better for less data storage in database, maybe I'm wrong. but I think your solution is better than mine. Thank you for your help.
but your approach stores MORE data in the database
In this case yes it would be better to store it on the users table since that will store less data. If you had not abbriviated the Man to M and Woman to F then it would have been better to have it in it's own table.
@tray2 @snapey @zizi_ove I have one doubt what if? Suppose I have users table and media table. In my users table I have media_id which is foreign key. Then What if suppose user register the table and this error through related to media_id does not have default value. Then How can I set to this. If foreign key is not default Null?
#This type of error I'm getting because in my users table there is photo_id column which is foreign key.
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'photo_id' doesn't have a default value (SQL: insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (Maggy Ellis, [email protected], y$wZpavxvwp/bdTv3wpxoZFOuMMYMy8X/.lDZqnLZwYGOc/C9a/JTY6, 2020-05-10 11:29:45, 2020-05-10 11:29:45))
#And this is my migration table code users table.
$table->foreignId('photo_id')->constrained();
Then how to get rid of this situation if Making a foreign key nullable defeats the purpose of it
Think of it the other way around. The photo should have a user_id, then the user can have zero or many photos.
So put the user_id in the photos table instead of photo_id in the users table.
@tray2 Okk! but what if this media table is used for post also then how can I specify for all. means media table is used post also. So in this schema if I set default('avatar) will work?
$table->foreignId('photo_id')->constrained()->default('avatar');
I have try this but not working
polymorphic relationships...
https://laravel.com/docs/7.x/eloquent-relationships#one-to-one-polymorphic-relations
This is how Spatie media library works. There are no media columns added to the things that might need images. The images table stores the type and the id of the model it relates to
@tray2 @snapey I have two tables: one for users with user.id, and another for tasks with participant_id (a foreign key referencing user.id) to store daily tasks. Most of the time, a task will be assigned to someone, but occasionally, it may not be assigned to anyone. What's the best approach for referencing the user table in this scenario?
@lantd That depends, is it only one person who can have a task, or can they share tasks?
I would probably use a third table to store the user_id and the task_id regardless if there is one or more persons for each task, that way I don't need to make the foreign key nullable.
Please or to participate in this conversation.