我刚刚开始使用 Laravel 媒体库。将图像上传到数据库时出现错误。我尝试在文档中搜索但我没有找到答案。我收到的错误Argument 1 passed to Spatie\MediaLibrary\MediaCollections\FileAdder::processMediaItem() must be an instance of Spatie\MediaLibrary\HasMedia, instance of App\Service given, called in C:\xampp\htdocs\Matheen\furniture_backend\vendor\spatie\laravel-medialibrary\src\MediaCollections\FileAdder.php on line 372控制器public function store(Request $request) { $service = Service::create([ 'service_name' => $request->service_name ]); $file = $request->file('image'); $service->addMedia($file)->toMediaCollection('services'); return redirect('services')->with('success','Service Added Successfully'); }模型use Illuminate\Database\Eloquent\Model;use Spatie\MediaLibrary\MediaCollections\Models\Media;use Spatie\MediaLibrary\InteractsWithMedia;use Spatie\MediaLibrary\HasMedia;class Service extends Model{ use InteractsWithMedia; protected $fillable = ['service_name']; public function registerMediaCollections(): void { $this->addMediaCollection('services'); } public function registerMediaConversions(Media $media = null): void { $this->addMediaConversion('thumbnail') ->width(1000) ->height(250); }}
2 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
在模型类声明中,您有:“类服务扩展模型”您需要:“类服务扩展模型实现 HasMedia”
...
...
use Spatie\MediaLibrary\HasMedia;
class Service extends Model implements HasMedia
{
// code
}
..
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
我做了这个
composer require spatie/laravel-medialibrary:10.0.7
问题已解决 laravel 9 medialibrary 10.0.7
- 2 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消