I need help understanding why computed fields from an afterStateUpdated
hook are not getting sent to the database.
I’m working on a website that has song lyrics. The backend admin is using Filament 3.x and is working fine. I’m working on adding the functionality for attaching online media (e.g. YouTube videos) that can be embedded on the song lyrics page. The functionality I want in the admin is to simply put in the youtube URL and using a MediaService
with 3rd party libraries, parse out the URL, grab the video title and other metadata from YouTube, etc. in the filament afterStateUpdated
hook.
That all seems to be working.
However, when I try to create and save to the database, I get an error that indicates that only the updated_at
and created_at
are being sent to the database with the error:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: media.host (Connection: sqlite, SQL: insert into "media" ("updated_at", "created_at") values (2024-06-18 15:55:09, 2024-06-18 15:55:09))
Here is the Media
model:
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Media extends Model
{
use HasFactory;
protected $fillable = ['host', 'media_id', 'url', 'title'];
}
Here is the migration for the media
table:
public function up(): void
{
Schema::create('media', function (Blueprint $table) {
$table->id();
$table->foreignId('song_id')
->nullable()
->constrained()->
onDelete('cascade');
$table->string('host');
$table->string('media_id');
$table->string('title')->nullable();
$table->string('url')->nullable();
$table->timestamps();
});
}
And the form
from the Filament MediaResource
public static function form(Form $form): Form
{
return $form
->schema([
FormsComponentsTextInput::make('input_url')
->label('Media URL')
->live(onBlur: true)
->afterStateUpdated(function (FormsSet $set, $state) {
$mediaService = new MediaService();
$parsed = $mediaService->parseUrl($state);
if ($parsed) {
$set('title', $parsed['title']);
$set('host', $parsed['host']);
$set('media_id', $parsed['media_id']);
$set('url', $parsed['url']);
}
}),
FormsComponentsTextInput::make('title')
->required()
->disabled(),
FormsComponentsTextInput::make('host')
->required()
->disabled(),
FormsComponentsTextInput::make('media_id')
->required()
->disabled(),
FormsComponentsTextInput::make('url')
->required()
->disabled(),
]);
}