filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.39k stars 2.97k forks source link

Can't upload file with SpatieMediaLibraryFileUpload through the relation manager #14858

Open firatdgn opened 1 day ago

firatdgn commented 1 day ago

Package

filament/filament

Package Version

v3.2.124

Laravel Version

10.48.24

Livewire Version

No response

PHP Version

PHP 8.2.25

Problem description

I have CustomerResource to handle Customer CRUD. I want to implement that File upload capability for the customers.I have installed Spatie Media Library plugin then created a relation manager. My purpose is to upload file with relation manager. I have set the resource form. It is listed as expected but, when I upload file and it throws SQL error:

image

Expected behavior

I was expecting to see upload file the local and insert data to table

Steps to reproduce

<?php

namespace App\Filament\Resources\CustomerResource\RelationManagers;

use App\Models\Customer;
use App\Models\Media;
use Filament\Forms;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class MediaRelationManager extends RelationManager
{
    protected static string $relationship = 'media';
    protected static ?string $title = 'Dosyalar';
    protected static ?string $modelLabel = 'Dosya';
    protected static ?string $pluralModelLabel = 'Dosyalar';

    public function isReadOnly(): bool
    {
        return false;
    }
    public function form(Form $form): Form
    {
        return $form
            ->schema([
                SpatieMediaLibraryFileUpload::make('attachment')->collection('test')->multiple(),
            ])
            ->model($this->getOwnerRecord())
        ;
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('name')
            ->columns([
                Tables\Columns\TextColumn::make('name'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}

I went to customer view page and try to upload file

Reproduction repository (issue will be closed if this is not valid)

https://github.com/firatdgn/filament-issue

Relevant log output

No response

Donate 💰 to fund this issue

Fund with Polar

abbasmashaddy72 commented 1 hour ago

I have successfully implemented a working RelationManager without using the ->model($this->getOwnerRecord()) method in the form, and it saves data perfectly. If it is still not working for you, you can try the following configuration for your file upload field:

SpatieMediaLibraryFileUpload::make('upload_quote')
    ->label(__('inba::labels.upload_quote'))
    ->collection('upload_quote')
    ->acceptedFileTypes(['application/pdf', 'image/jpeg', 'image/png', 'image/webp'])
    ->helperText('.pdf, .jpg, .jpeg, .png, .webp')
    ->statePath('data'); // Map the state to the 'data' attribute

@firatdgn