Spatie\MediaLibrary error, argument #1 ($model) must be of type Spatie\MediaLibrary\HasMedia, App\Models\Post given

huangapple go评论63阅读模式
英文:

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 = [
		&#39;title&#39;, &#39;body&#39;,
	];
}

Controller:

public function postUploadImage(Request $request, Post $post) 
{
	$request-&gt;validate([
		&#39;image&#39; =&gt; &#39;required|image|mimes:jpeg,png,jpg,gif|max:200&#39;,
	]);
	$post-&gt;addMediaFromRequest(&#39;image&#39;)-&gt;toMediaCollection(&#39;images&#39;);
	return back();
}

Form:

&lt;form action=&quot;{{ route(&#39;admin.postUploadImage&#39;, $post-&gt;id) }}&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
	@csrf
	@method(&#39;PATCH&#39;)
	&lt;input type=&quot;file&quot; name=&quot;image&quot;&gt;
	&lt;button type=&quot;submit&quot;&gt;Upload Image&lt;/button&gt;
&lt;/form&gt;

Route:

Route::patch(&#39;/post-upload-image/{post}&#39;, [PostController::class, &#39;postUploadImage&#39;])-&gt;name(&#39;postUploadImage&#39;);

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;
}

huangapple
  • 本文由 发表于 2023年5月29日 23:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定