我正在使用laravel 8构建一个API。我想为我的帖子存储图片或图片(因为我也有用户和分析表,我也需要图片),但当我在postman中发送值时,我不能上传和存储图片,如下所示:

正如您所看到的,图像没有存储在图像表中,因此我看不到关于图像的任何内容
我是初学者,对多态性了解不多,所以我认为我的store()方法是不正确的。
这是我的帖子表:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->longText('body');
$table->string('video')->nullable();
$table->string('study_time');
$table->integer('likes')->nullable();
$table->tinyInteger('status')->nullable()->comment('status is 1 when a post is active and it is 0 otherwise.')->nullable();
$table->text('tags')->nullable();
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('user_id')->references('id')->on('users');
});和我的image表:
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->integer('imageable_id');
$table->string('imageable_type');
$table->string('url');
$table->timestamps();
});和post模型:
.
.
.
.
public function image(){
return $this->morphOne(Image::class , 'imageable');
}和我的图像模型:
protected $fillable = [
'url'
];
public function imageable(){
return $this->morphTo();
}和postController中的my store()方法:
public function store(Request $request )
{
$post = new Post;
$post->category_id = $request->get('category_id');
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->study_time = $request->get('study_time');
$post->tags = $request->get('tags');
$post->user_id = JWTAuth::user()->id;
$tags = explode(",", $request->tags);
$post->tag($tags);
if($request->hasfile('url'))
{
foreach($request->get('url') as $file)
{
$image = new Image;
$name = $file->getClientOriginalName();
$image->move(public_path().'/images/',$name);
$post->photos()->save($image);
}
}
$post->save();
return response()->json($post , 201);
}同样在postman的form-data中,我输入图像作为文件类型
在headers part I中,输入带有multipart/form-data值的Content-Type和
我不知道如何保存图片,谢谢你的帮助。
编辑:
我将控制器更改为:
public function store(Request $request )
{
$post = new Post;
$post->category_id = $request->get('category_id');
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->study_time = $request->get('study_time');
$post->tags = $request->get('tags');
$post->user_id = JWTAuth::user()->id;
$tags = explode(",", $request->tags);
$post->tag($tags);
$allowedfileExtension=['pdf','jpg','png'];
$files = $request->file('fileName');
foreach ($files as $file) {
$extension = $file->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if($check) {
foreach($request->fileName as $mediaFiles) {
$url = $mediaFiles->store('public/images');
//store image file into directory and db
$image = new Image();
$image->url = $url;
}
}
else {
return response()->json(['invalid_file_format'], 422);
}
}
$post->image()->save($image);
$post->save();
return response()->json($post , 201);
}在postman中,我在body中输入带有表单字段的图像( url -data):

所以我的foreach($files as $file)有一个错误,是什么问题?
发布于 2021-04-03 18:35:20

尝试发送多张图片,如截图所示,您正在发送没有名称的图片,因此请求无法获取保存该图片的密钥,对于发送多张图片,您可以像在屏幕截图中那样使用数组(image[]),而对于发送单个图片,您可以像其他任何普通字段名称一样使用单个字段名
https://stackoverflow.com/questions/66930049
复制相似问题