如何在laravel中更新文件

时间:2018-06-28 08:19:22

标签: laravel laravel-5.5

我更新了两个数据,一个是文本数据,另一个是两个文件数据,但是不能正常工作。如何更新文件?

我的代码是:

public function update(Request $request){
    $news=new News();
    $news->newstitle=$request->newstitle;
    $url1=$this->imageExistStatus1($request);
    $news->save();
    return redirect()->back()->with('sms','insert successful');
}

public  function imageExistStatus1($request){
    $newsByid1=News::where('id',$request->newsid)->first();
    $fimage1=$request->file('imageone');

    if ($fimage1) {
        unlink($newsByid1->imageone)
        $thisName1= $fimage1->getClientOriginalName();
        $uplodePath1='public/up/';
        $fimage1->move($uplodePath1,$thisName1);
        $url1=$uplodePath1.$thisName1;
    }else{
        $url1=$newsByid1->imageone;
    }

    return $url1;
}

1 个答案:

答案 0 :(得分:0)

首先,将您的代码更改为以下

   public function update(Request $request, $id)
{
    $news = News::findOrFail($id);
    $news->newstitle = $request->newstitle;
    $fimage1 = $request->file('imageone');
    if ($fimage1) {
        unlink($news->imageone);
        $news->imageone = $this->imageExistStatus1($request, $id);
    }
    $news->save();
    return redirect()->back()->with('sms', 'insert successful');

}

public function imageExistStatus1($request)
{
    $fimage1 = $request->file('imageone');
    $thisName1 = $fimage1->getClientOriginalName();
    $uplodePath1 = 'public/up/';
    $fimage1->move($uplodePath1, $thisName1);
    $url1 = $uplodePath1 . $thisName1;

    return $url1;

}

希望您的问题得到解决

提示:

感谢“ Oluwatobi Samuel Omisakin”。

请使用以下路线:

Route::patch('/news/{id}/update', 'NewsController@update')