问题:我如何将路径存储在桌面帖子上 - >路径
$ path = public_path(' image')。$ imageName;没有工作
控制器
public function store(Request $request)
{
$this->validate(request(),[
'title' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg',
'body' => 'required',
]);
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('image'), $imageName);
auth()->user()->publish(
new Post(request(['title','body', 'path']))
);
session()->flash('message', 'your post has now been published');
return redirect('/');
}
答案 0 :(得分:0)
这就是我接近它的方式:
您的验证请求错误,而request()
未见下文:
public function store(Request $request, Post $post)
{
$this->validate($request,[
'title' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg',
'body' => 'required',
]);
if($files=$request->file('image')){
$path = public_path('path/to/image/folder');
$name= $files->getClientOriginalName();
$files->move($path, $name);
}
$userid = Auth::user()->id;
$post->create([
'user_id' => $userid,
'title' => $request->get('title'),
'body' => $request->get('body'),
'path' => $name (for image)
]);
session()->flash('message', 'your post has now been published');
return redirect()->back();
}
如果您希望存储路径,请将$ name更改为$ path,然后为您保存路径。
注意:可能需要更改$request->get()
以适合表单中的name=" "
字段。
答案 1 :(得分:0)
我会这样做,我觉得它更简单
try {
$url = 'http://localhost:8000/storage/' . $request->file('picfile')->store('uploads/UserImage', 'public');
} catch (\Exception $e) {
return response()->json([$e->getMessage()], 501);
}
上传"上传/用户图片"是存储它的文件夹
而$ url我只是网址
答案 2 :(得分:0)