How to Serve Trix Editor Files Publicly in Laravel Nova
Learn how to make files uploaded through the Trix editor in Laravel Nova publicly accessible. This step-by-step guide covers configuring your filesystem, setting up the Trix editor, and generating public URLs for seamless access to your content.

Step 1: Configure S3
First, ensure your S3 configuration is set up correctly. Set up your S3 bucket and grab your credentials, then use those details for the following environment variables in your .env file:
AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION= AWS_BUCKET= AWS_USE_PATH_STYLE_ENDPOINT=
Trix::make('Content')
->withFiles('s3', 'blog-images')
->sortable()
->rules('required'),public function getContentAttribute($value): ?string
{
if (!$value) {
return null;
}
$dom = new \DOMDocument();
$dom->loadHTML($value, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$hrefs = $dom->getElementsByTagName('a');
$images = $dom->getElementsByTagName('img');
foreach ($hrefs as $href) {
$value = $href->getAttribute('href');
if (Str::startsWith($value, 'https://your-s3-bucket-url')) {
$formattedValue = Str::after($value, 'https://your-s3-bucket-url');
$href->setAttribute('href', Storage::disk('s3')->temporaryUrl($formattedValue, now()->addMinutes(5)));
}
}
foreach ($images as $img) {
$src = Str::after($img->getAttribute('src'), 'https://your-s3-bucket-url');
$img->setAttribute('src', Storage::disk('s3')->temporaryUrl($src, now()->addMinutes(5)));
}
return $dom->saveHTML();
}Step 4: Understanding the Code
if (!$value) {
return null;
}$dom = new \DOMDocument();
$dom->loadHTML($value);
$hrefs = $dom->getElementsByTagName('a');
$images = $dom->getElementsByTagName('img');foreach ($hrefs as $href) {
$value = $href->getAttribute('href');
if (Str::startsWith($value, 'https://your-s3-bucket-url')) {
$formattedValue = Str::after($value, 'https://your-s3-bucket-url');
$href->setAttribute('href', Storage::disk('s3')->temporaryUrl($formattedValue, now()->addMinutes(5)));
}
}foreach ($images as $img) {
$src = Str::after($img->getAttribute('src'), 'https://your-s3-bucket-url');
$img->setAttribute('src', Storage::disk('s3')->temporaryUrl($src, now()->addMinutes(5)));
}return $dom->saveHTML();