I assume that you have set up s3 disk such as by using AWS S3 or its compatible alternative Digital Ocean Spaces / Vultr Object Storage. If you have not then you can refer to our tutorials on it by searching our post list.
Using Default "public" Disk
The default public disk will have the following configuration and this is generally will be stored inside the "storage" directory itself.
<?php // filesystem.php 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
<?php // app/Nova/Post.php Trix::make(__('Body'), 'body') ->withFiles('public') ->rules('required'),
Using "s3" Disk
If you are using s3 disk then you can specify it like below. But do note that by default it will be stored as "private" file and won't be accessible by everyone.
<?php // app/Nova/Post.php Trix::make(__('Body'), 'body') ->withFiles('s3') ->rules('required'),
storePublicly()
<?php 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), ]
<?php Trix::make(__('Body'), 'body') ->withFiles('s3') ->attach(function (NovaRequest $request) { $url = $request->file('attachment')->storePublicly('/your-subdirectory/'); return Storage::url($url); }) ->rules('required')
<?php Trix::make(__('Body'), 'body')->attach(function (NovaRequest $request) { // First do extract the file name and extension (the default file name is 'attachment') [$name, $extension] = explode('.', $request->file('attachment')->getClientOriginalName()); // Generate the slug and concatenate it with the extension $filename = Str::slug($name) . '.' . $extension; // Store the file name in s3 $request->file('attachment')->storePubliclyAs('/your-subdirectory/', $filename); // Return the url for the newly saved file return 'https://your-site.com/' . $filename; }),
Leave a reply