Laravel用户发布图片上传错误

时间:2018-02-26 03:15:11

标签: image laravel post upload

我上传Post Image时出错我可以在我的电脑上保存图片,但在我的数据库中文件名失真。

这是我的PostsController

public function store(Request $request, User $user, Image $image)
{
    $this->validate($request, [
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'body' => 'required'
    ]);


    if( $request->hasFile('image') ) {
        $image = $request->file('image');
        $filename = time() . '.' . $image->getClientOriginalExtension();

        Image::make($image)->save( public_path('uploads/images/' . $filename ) );
    }

    $image = $filename;

    auth()->user()->publish(
        new Post(request(['body', 'image'])));

    return redirect('/');
}

filename is wrong

1 个答案:

答案 0 :(得分:0)

问题是您尝试将错误的变量插入数据库表中。所以,像这样改进你的代码:

auth()->user()->publish(
    new Post(['photo' => request('body'), 'image' => $image])
);