如何在laravel中以编辑条件获取图像URL

时间:2019-02-19 07:22:34

标签: php laravel

我正在使用Laravel开发CRUD应用程序,并且尝试在更新条件下更新数据,但是如果不更新新图像,则会出现“文件名字段为必填字段。” 再次选择新图像。请帮助我如何称呼旧图像。这是我的代码:

这是我在控制器中的商店更新功能

public function store(Request $request)
{
    $this->validate($request, [
        'share_name'=>'required',
        'share_price'=> 'required|integer',
        'share_qty' => 'required|integer',
        'filename' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $share = new Share;
    $share->share_name = $request->share_name;
    $share->share_price = $request->share_price;
    $share->share_qty = $request->share_qty;

    if($request->hasFile('filename')){

        $image= $request->file('filename');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $filename);
        $share->filename= $filename;
        $share->save();
    };

    $share->save();
    return redirect('/shares')
        ->with('success','Stock added successfully.');
}
public function update(Request $request, $id)
{
    $this->validate($request, [
        'share_name'=>'required',
        'share_price'=> 'required|integer',
        'share_qty' => 'required|integer',
        'filename' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $share = Share::find($id);
    $share->share_name = $request->share_name;
    $share->share_price = $request->share_price;
    $share->share_qty = $request->share_qty;
    if($request->hasFile('filename')){

        $dir = 'images/'.$share->filename;
        if (File::exists($dir)){
             File::delete($dir);
        };
        $image= $request->file('filename');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $filename);
        $share->filename= $filename;
        $share->save();
    };

    $share->save();

  return redirect('/shares')->with('success', 'Stock has been updated');
}

这里是视图

    <div class="form-group">
      <label for="image">Share Image : </label><br/>
      <input type="file" name="filename" id="aa" onchange="pressed()" value={{ $share->share_qty }}/>

1 个答案:

答案 0 :(得分:1)

您不能在输入文件标签上传递值。在Controller更新方法上检查验证是否存在文件。看看我的答案。

public function update(Request $request, $id){
    $this->validate($request, [
        'share_name'=>'required',
        'share_price'=> 'required|integer',
        'share_qty' => 'required|integer'
    ]);

    if($request->hasFile('filename')){
        $this->validate($request, [
            'filename' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
    }

    $share = Share::find($id);
    $share->share_name = $request->share_name;
    $share->share_price = $request->share_price;
    $share->share_qty = $request->share_qty;
    if($request->hasFile('filename')){
        $dir = 'images/'.$share->filename;
        if (File::exists($dir)){
             File::delete($dir);
        };
        $image= $request->file('filename');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $filename);
        $share->filename= $filename;
        $share->save();
    };

    $share->save();

  return redirect('/shares')->with('success', 'Stock has been updated');
}