From 4e33dd9ec5694fa27652d842a16d05504e89a3a7 Mon Sep 17 00:00:00 2001 From: Refringe Date: Tue, 24 Sep 2024 00:46:44 -0400 Subject: [PATCH] Updates About Field Default Updates the about column to have a default of an empty string. Curse you, MySQL! --- app/Models/User.php | 19 +++++++++++++++++++ .../0001_01_01_000000_create_users_table.php | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index 4c268c4..8acc7db 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,6 +8,7 @@ use App\Notifications\VerifyEmail; use App\Traits\HasCoverPhoto; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -209,6 +210,24 @@ class User extends Authenticatable implements MustVerifyEmail return $filters->apply($builder); } + /** + * Handle the about default value if empty. Thanks MySQL! + */ + protected function about(): Attribute + { + return Attribute::make( + set: function ($value) { + // MySQL will not allow you to set a default value of an empty string for a (LONG)TEXT column. *le sigh* + // NULL is the default. If NULL is saved, we'll swap it out for an empty string. + if (is_null($value)) { + return ''; + } + + return $value; + }, + ); + } + /** * Get the disk that profile photos should be stored on. */ diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php index 1e03166..73c418b 100644 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ b/database/migrations/0001_01_01_000000_create_users_table.php @@ -22,7 +22,7 @@ return new class extends Migration $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); - $table->longText('about'); + $table->longText('about')->nullable()->default(null); $table->foreignIdFor(UserRole::class) ->nullable() ->default(null)