英文:
Spatie\MediaLibrary error, argument #1 ($model) must be of type Spatie\MediaLibrary\HasMedia, App\Models\Post given
问题
Laravel的Spatie Media Library出现问题。使用的是Laravel 10.10和Spatie Media版本10.0。
我按照官方文档中的描述做了一切。
模型:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Post extends Model
{
use InteractsWithMedia;
protected $fillable = [
'title', 'body',
];
}
控制器:
public function postUploadImage(Request $request, Post $post)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:200',
]);
$post->addMediaFromRequest('image')->toMediaCollection('images');
return back();
}
表单:
<form action="{{ route('admin.postUploadImage', $post->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PATCH')
<input type="file" name="image">
<button type="submit">上传图片</button>
</form>
路由:
Route::patch('/post-upload-image/{post}', [PostController::class, 'postUploadImage'])->name('postUploadImage');
错误:
Spatie\MediaLibrary\MediaCollections\FileAdder::processMediaItem(): 参数#1($model)必须是Spatie\MediaLibrary\HasMedia类型,给定的是App\Models\Post,调用位置:C:\Users\Administrator\Desktop\laravel-example\vendor\spatie\laravel-medialibrary\src\MediaCollections\FileAdder.php,第428行
英文:
Laravel Spatie Media Library is not working. Using Laravel 10.10 and spatie media 10.0.
I did all the things as described at official docs.
Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Post extends Model
{
use InteractsWithMedia;
protected $fillable = [
'title', 'body',
];
}
Controller:
public function postUploadImage(Request $request, Post $post)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:200',
]);
$post->addMediaFromRequest('image')->toMediaCollection('images');
return back();
}
Form:
<form action="{{ route('admin.postUploadImage', $post->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PATCH')
<input type="file" name="image">
<button type="submit">Upload Image</button>
</form>
Route:
Route::patch('/post-upload-image/{post}', [PostController::class, 'postUploadImage'])->name('postUploadImage');
Error:
> Spatie\MediaLibrary\MediaCollections\FileAdder::processMediaItem(): Argument #1 ($model) must be of type Spatie\MediaLibrary\HasMedia, App\Models\Post given, called in C:\Users\Administrator\Desktop\laravel-example\vendor\spatie\laravel-medialibrary\src\MediaCollections\FileAdder.php on line 428
答案1
得分: 2
你没有正确准备你的model,它缺少了implements
运算符。
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
}
英文:
You didn't prepare your model correctly, it's missing the implements
operator.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论