how to make a varchar nullable and unique together in mysql (laravel) how to make a varchar nullable and unique together in mysql (laravel) php php

how to make a varchar nullable and unique together in mysql (laravel)


I know this is an old question, this is for others who have the same issue.

You are trying to add an empty string. You made column nullable and unique. That means you are trying something you shouldn't. You can send multiple null values but not mutiple empty string. MySQL consider empty as a value.

or you could write a mutator in your modal

public function setEmailAttribute($value) {    if ( empty($value) ) { // will check for empty string    $this->attributes['email'] = NULL;    } else {        $this->attributes['email'] = $value;    }}


We just make a mistake when have to insert null. In case of null we have to declare null like this:

if(empty($request->email_id)){    $user->email_id = NULL;}

Don't use "NULL", this will be considered as string.