Created
March 8, 2023 00:38
-
-
Save tacocode-dev/1ae0ece177fb80b0971b346d6593c1d6 to your computer and use it in GitHub Desktop.
Laravel Livewire WireConvertEmptyStringsToNull Trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
/** | |
* Trait WireConvertEmptyStringsToNull | |
* | |
* @link https://github.com/livewire/livewire/issues/823 | |
*/ | |
trait WireConvertEmptyStringsToNull | |
{ | |
/** | |
* @var string[] | |
*/ | |
protected array $convertEmptyStringsExcept = [ | |
// | |
]; | |
/** | |
* @param string $name | |
* @param mixed $value | |
*/ | |
public function updatedWireConvertEmptyStringsToNull(string $name, mixed $value): void | |
{ | |
if (! is_string($value) || in_array($name, $this->convertEmptyStringsExcept)) { | |
return; | |
} | |
$value = ltrim($value); | |
$value = $value === '' ? null : $value; | |
data_set($this, $name, $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment