Laravel 为什么图像不是';t存储在公共路径中

Laravel 为什么图像不是';t存储在公共路径中,laravel,api,postman,Laravel,Api,Postman,我是一个Laravel初学者,我想用Laravel 8构建一个API 我有帖子和图片,我想存储和更新它们 我的store方法有效,图像保存在数据库和public路径的图像文件夹中,但在update方法中,我无法将其保存在文件夹中 这是我的密码: PostController 公共函数存储(请求$Request) { $data=$request->all(); //验证帖子和图像字段 $validator=validator::make($data[ 'user_id'=>'required',

我是一个Laravel初学者,我想用Laravel 8构建一个API

我有
帖子
图片
,我想存储和更新它们

我的
store
方法有效,图像保存在数据库和
public
路径的图像文件夹中,但在
update
方法中,我无法将其保存在文件夹中

这是我的密码:

PostController
公共函数存储(请求$Request)
{
$data=$request->all();
//验证帖子和图像字段
$validator=validator::make($data[
'user_id'=>'required',
“类别id”=>“必需”,
“标题”=>“必需”|最多:150 |唯一:帖子”,
“正文”=>“必需”,
“学习时间”=>“必需”,
'tags'=>'nullable | string',
'图像'=>'必需',
]);
如果($validator->fails()){
return response()->json(['error'=>$validator->errors(),“error”);
}
//单独标签
$tags=explode(“,”,$request->tags);
如果($request->hasfile('image')){
//从请求中获取post图像
$files=$request->file('image');
//保存图像的名称和路径
foreach($files作为$file){
$imageName=time().rand(110000)。“.$file->extension();
$postTitle=$request->title;//发布文件夹名称及其内部图像的标题
$imagePath=public_path()。/images/posts/。$postTitle;
$file->move($imagePath,$imageName);
$image=新图像;
$image->image=$imageName;
$image->path=$imagePath;
$images[]=$image;//创建上载图像的数组
}
}
$post=post::创建($data);
$post->images()->saveMany($images);//在图像表中将图像另存为
$post->tag($tags);//在tags表中保存标记
返回响应()->json([
“成功”=>正确,
“消息”=>“消息”,
“数据”=>$post
]);
}
公共功能更新(请求$Request,$id)
{
$post_failed=post::find($id);
如果(为空($post_失败)){
返回响应();
}
$data=$request->all();
//验证帖子和图像字段
$validator=validator::make($data[
'user_id'=>'required',
“类别id”=>“必需”,
“标题”=>“必需”|最多:150 |唯一:帖子”,
“正文”=>“必需”,
“学习时间”=>“必需”,
'tags'=>'nullable | string',
'图像'=>'必需',
]);
如果($validator->fails()){
return response()->json(['error'=>$validator->errors(),“error”);
}
$tags=explode(“,”,$request->tags);
如果($request->hasfile('image')){
$postTitle=$request->title;//发布文件夹名称及其内部图像的标题
//从数据库中删除最后一个图像以更新图像
Image::where('imageable_-type','App\Models\Post')->where('imageable_-id',$id)->delete();
//删除最后的图像文件夹
文件::删除(公共路径('/images/posts/'.$postTitle));
$files=$request->file('image');
foreach($files作为$file){
$imageName=time().rand(110000)。“.$file->extension();
$imagePath=public_path()。/images/posts/。$postTitle;
$image=新图像();
$image->image=$imageName;
$image->path=$imagePath;
$images[]=$images;
}
}
$post=post::find($id);
$post->user_id=$data['user_id'];
$post->category_id=$data['category_id'];
$post->title=$data['title'];
$post->body=$data['body'];
$post->study_time=$data['study_time'];
$post->tags=$data['tags'];
$post->save();
$post->images()->saveMany($images);
$post->tag($tags);
返回响应()->json([
“成功”=>正确,
“消息”=>“消息”,
“数据”=>$post
]);
}
posts
images
之间的关系是一对多的,我用postman测试了它

邮递员

数据库

路径是:

请提供帮助。

store()
方法中,您使用

$imagePath = public_path(). '/images/posts/'.$postTitle;
$file->move($imagePath, $imageName);
update()
中,您删除了它们

File::delete(public_path('/images/posts/'.$postTitle));
并确定新文件的路径

$imagePath = public_path(). '/images/posts/'.$postTitle;
但这之后什么也没发生。在整个
update()

因此,再次使用
$file->move()
存储
facade保存文件

提示
同样,重复这样的长代码逻辑也是不好的做法。最好将其提取出来并在存储/更新之间共享。

“但在更新方法中,我无法将其保存在文件夹中。”请对此进行解释。如果我删除这行
File::delete(public_path('/images/posts/'.$postitle)),它就不会出现在其他地方,也不会出现任何地方,日志中是否有错误、警告或其他信息?我的意思是,如果我删除这行
File::delete(public_path('/images/posts/'.$postitle))不再显示任何内容。这个
文件::DLETE
根本不起作用