Skip to content

Instantly share code, notes, and snippets.

@tacocode-dev
Created March 8, 2023 00:38
Show Gist options
  • Save tacocode-dev/1ae0ece177fb80b0971b346d6593c1d6 to your computer and use it in GitHub Desktop.
Save tacocode-dev/1ae0ece177fb80b0971b346d6593c1d6 to your computer and use it in GitHub Desktop.
Laravel Livewire WireConvertEmptyStringsToNull Trait
<?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