skip to Main Content

I am using FilamentPHP and Spatie media library. Additionally, I am using the Spatie media library FilamentPHP plugin in order to show and upload the images in my admin panel.

However, the images are not being uploaded and stored at all!

This is my model:

use IlluminateDatabaseEloquentModel;
use SpatieMediaLibraryHasMedia;
use SpatieMediaLibraryInteractsWithMedia;

class OrderItem extends Model implements HasMedia
{
    use InteractsWithMedia;

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('designs')
            ->useDisk('designFiles');
    }
}

My filesystem.php:

'designFiles' => [
    'driver' => 'local',
    'root' => storage_path('app/public/designs'),
    'url' => env('APP_URL') . '/storage/designs',
    'visibility' => 'public',
],

My filament resource:

SpatieMediaLibraryFileUpload::make('design')
    ->collection('designs')
    ->downloadable()
    ->multiple(),

The image I upload on my filament resource are being stored temporarily at storage/app/livewire-tmp. However, there is no entry in my spatie media library media table and the image is also not being saved to the desired disk (designFiles).

I have already tried to debug this problem but there is no error event being logged at all. I am using Laravel Valet and I have also added the port to my APP_URL variable with my .env file as suggested here but it did not help me.

I have also run php artisan storage:link but because I did run this command before already, it did not change anything. The image is still not being saved.

Those are the versions I use:

"php": "^8.2",
"filament/filament": "^3.2",
"filament/spatie-laravel-media-library-plugin": "^3.2",
"laravel/framework": "^11.9",
"spatie/laravel-medialibrary": "^11.8"

Anybody has an idea how to fix this problem?

Kind regards

2

Answers


  1. 1. In your controller or a tinker session, try something like this:

    $orderItem = OrderItem::find(1); // OrderItem ID
    $orderItem->addMedia($pathToFile) // path to a file
        ->toMediaCollection('designs');
    

    2.Also check the configuration

    SpatieMediaLibraryFileUpload::make('design')
        ->collection('designs')
        ->disk('designFiles')
        ->downloadable()
        ->multiple(),
    

    3.Try to check storage permissions

    sudo chmod -R 775 storage
    sudo chown -R $USER:www-data storage
    

    In this you can create debug log as well to check and verify that Filament resource saves the data.

    Please let me know if this could help you.

    Login or Signup to reply.
  2. This is not exactly a solution as I do not have a clear picture of your precise setup, but it may help you track down the problem.

    I applied the parameters that you specified and my media files were successfully saved to the correct location (app/public/designs). I however used "spatie/laravel-medialibrary": "^11.9"

    Add the following code to have a finer-grained view of what is going on when you save an OrderItem:

    app/Providers/AppServiceProvider.php

    namespace AppProviders;
    
    use IlluminateSupportServiceProvider;
    use IlluminateSupportFacadesDB;
    use IlluminateSupportFacadesLog;
    use IlluminateDatabaseEventsQueryExecuted;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         */
        public function boot(): void
        {
            DB::listen(function(QueryExecuted $query) {
                $is_insert = strpos($query->sql, "insert into") !== false;
                $is_orderitem = strpos($query->sql, "order_items") !== false;
                $is_mediaitem = strpos($query->sql, "media") !== false;
                if ($is_insert && ($is_orderitem || $is_mediaitem)) {
                    Log::info(
                        $query->sql,
                        [
                            'bindings' => $query->bindings,
                            'time' => $query->time
                        ]
                    );
                }
                
            });
        }
    }
    

    This should log any queries that insert to order_items or a media. I’ve assumed these are the names you have for the matching tables in your database. If the names you are using are different, then update the code accordingly.

    The logged transactions can be viewed in storage/logs/laravel.log. Newer log entries are at the very end of the file.

    In the case of a successful creation of an OrderItem, the log file should contain lines beginning with:

    • [TIME_STAMP] local.INFO: insert into 'order_items'...
    • [TIME_STAMP] local.INFO: insert into 'media'...

    The media file gets copied to app/public/designs after insert into 'media'... has succeeded. Lastly, the uploaded temporary file is deleted from storage/app/livewire-tmp.

    With these insights, you can figure out at what point the problem is occurring. If order_items has a new entry, but there isn’t a matching one in media, then the process failed before handling the media.

    My best guess is that you have a file permissions problem that prevents the copying of storage/app/livewire-tmp/image.png to app/public/designs/image.png. And just to be sure, you can try to save an OrderItem that does not have any media fields so as to establish there isn’t any other non-media related issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search