【问题标题】:Can't update a photo in laravellaravel 无法更新照片
【发布时间】:2017-04-14 22:09:25
【问题描述】:

我的应用程序中的每个用户都有一张封面照片,就像 Facebook 一样。我已经尝试了好几天才能更新它,但现在我被卡住了。当我点击更新时,我的用户之前的照片会被删除,新照片不会出现,我的默认照片会出现。

这是我的控制器

    public function updateCover(Request $request, $id)
{
    $user = User::findorFail($id)->with('cover_id');

    $input = $request->all();

    $input = Input::file('cover_id');
    $filename  = time() . '.' . $input->getClientOriginalExtension();
    $path = public_path('images' . $filename);
    Photo::make($input->getRealPath())->save($path);
    $user->cover = 'images'.$filename;
    $user->save();
}

我对表单的看法

  {!! Form::model($user, ['method'=>'PUT', 'action'=>
   ['UserController@updateCover', $user->id], 'files'=>true]) !!}
   {!! Form::file('cover_id', ['class'=>'form-control']) !!}

我不是 laravel 方面的专家,所以就我所知,我可能完全错了,我只是想在更新时更改封面照片。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    用移动功能试试我的方法:

    $user = User::findorFail($id)->with('cover_id');
    $input = $request->all();
    $file = $request->file('input_img');
    $filename  =  str_random(40). '.' . $input->getClientOriginalExtension();
    $file->move("fotoupload", $filename);
    $user->cover = $filename;
    $user->save();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2013-03-30
      • 1970-01-01
      • 2018-12-18
      相关资源
      最近更新 更多