我刚刚开始使用 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);
}
}
慕桂英3389331
拉风的咖菲猫