使用avatar laravel发布更新用户帐户

时间:2018-04-29 11:21:36

标签: laravel laravel-5

所以我能够提交表格以允许上传和更改哪个有效的头像。现在我正在尝试允许更新所有用户详细信息。

如果我只是尝试更改用户名或电子邮件并提交,则会重新加载原始内容的页面。当我上传新的头像然后尝试提交时我得到错误&#34;非静态方法Illuminate \ Http \ Request :: input()不应该静态调用&#34; 。< / p>

*已编辑***我刚刚意识到我说如果用户上传图片然后更新表单。而我希望表单提交是否已上传图像。我怎么能改变我的update_avatar函数才能使它工作?

web.php

Route::get('profile','userController@profile');
Route::post('profile', 'userController@update_avatar');

UserController中

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

use Image;

class UserController extends Controller
{
    //
    public function profile()
    {
        return view('profile', array('user' => Auth::user()) );
    }

    public function update_avatar(Request $request)
    {
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );

            $user = Auth::user();
            $user->avatar = $filename;
            $user->name = Request::input('username');
            $user->email = Request::input('email');
            $user->save();

        }
        return view('profile', array('user' => Auth::user()) );
    }
}

profile.blade.php

<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">    
                <h2>{{ $user->name }}'s Profile</h2>

                <form enctype="multipart/form-data" action="/profile" method="post">
                    <label>Update Profile Image</label>
                    <input type="file" name="avatar">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">

                    <label>Username</label>
                    <input type="text" name="username" class="form-control" value="{{ $user->name }}">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" value="{{ $user->email }}">

                    <input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;">
                </form>

2 个答案:

答案 0 :(得分:0)

更改代码

$user->name = $request->input('username');
$user->email = $request->input('email');

请参阅文档以获取更多信息:

  

http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers

答案 1 :(得分:0)

在控制器的名称空间声明的顶部, 替换以下

use Illuminate\Http\Request;

use Request;