1

我可以将图像存储在所需位置,但无法查看。重新加载页面时,会出现相同的默认图像。默认图像永远不会更改为我想要的图像。

我的控制器文件(UserController.php):

     public function update_avatar(Request $request)
    {

      if($request->hasFile('avatar'))

      {

            $avatar = $request->file('avatar');
            $filename = time(). '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->save(public_path('/src/uploads/avatars/' . $filename));


      }

      return redirect()->route('dashboard');
    }




}

我的路线文件:

Route::post('/dashboard',[
                'uses'=>'UserController@update_avatar',

                ]);

我的观点文件:

<form action="/dashboard" method="post" enctype="multipart/form-data">
      <div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>

    <input type="file" name="avatar" class="btn btn-sm btn-primary col-md-5"  >

    <input type="submit" class="pull-right btn btn-sm btn-primary " value="submit">
    <input type="hidden" value="{{Session::token() }}" name="_token">
    <a href="#">Remove</a>

    </div>


        <div id="main">
<span style="font-size:30px;cursor:pointer" onclick="openNav()"><img src="download.jpg" class="img-circle img-responsive" alt="Placeholder image"></span>
</div>

      </form>
4

2 回答 2

0

使用头像属性更改用户适合您的需要

public function update_avatar(Request $request)
{
    if($request->hasFile('avatar'))
    {
        $avatar = $request->file('avatar');
        $path = '/src/uploads/avatars/';
        $filename = time(). '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->save(public_path($path . $filename));
        $request->user()->avatar = $filename;
        $request->user()->save();
    }
    return redirect()->route('dashboard');
}
于 2016-08-16T01:37:56.447 回答
0

您没有将上传的图像保存到数据库吗?您是否仅将图像上传到专用图像文件夹而不将其保存到数据库?如果需要,可以在控制器中执行此操作:

    <?php

        public function update_avatar(Request $request){
            $avatarURI = null;
            if($request->hasFile('avatar')) {

                $avatar     = $request->file('avatar');
                $filename   = time(). '.' . $avatar->getClientOriginalExtension();
                Image::make($avatar)->save(public_path('/src/uploads/avatars/' . $filename));
                $avatarURI  = "src/uploads/{$filename}";
                User::update();
            }

            // YOU MAY NEED TO PERSIST THIS IN THE DATABASE
            // TO UPDATE THE avatar:

            $usr = new \App\User();
            $usr->update(['avatar' => $avatarURI, 'id'=>$userID]); //<== ID OF THE USER TO BE UPDATED...
            return redirect()->route('dashboard', ['imgURI'=>$avatarURI, 'user'=>$usr]);
        }
于 2016-08-15T20:24:33.777 回答