Laravel在用户注册中添加头像

时间:2016-06-10 02:06:27

标签: php laravel

我现在有一张表格效果很好。基本上我是在添加用户并根据表单为他们分配角色。大。但是现在我想在“avatar”列下的用户表中添加一个头像。我发现的有意义但不起作用,得到错误“非静态方法Illuminate \ Http \ Request :: hasFile()不应该静态调用”

AuthController.php

//imports
use App\User;
use App\Role;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;

//create method
protected function create(array $data)
    {



        //Creat the user
        $create = User::create([
            'first_name' => $data['first-name'],
            'last_name' => $data['last-name'],
            'phone' => $data['phone'],
            'avatar' => $data['avatar'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        //Is it a User? Then give them that role
        if ($data['form'] == 'user')
        {
            $user = User::find($create->id);

            $role = Role::where('name', '=', 'user')->firstOrFail();

            $user->roles()->attach($role->id);
        }

        //Is it an Admin? Then give them that role
        if ($data['form'] == 'admin')
        {
            $user = User::find($create->id);

            $role = Role::where('name', '=', 'owner')->firstOrFail();

            $user->roles()->attach($role->id);
        }

        //Did they upload an Avatar
        if (Request::HasFile('avatar'))
        {

            $file = Request::file('avatar');
            $img_path   = '/uploads/avatars/';

            $image_name = time() . '-' . $file->getClientOriginalName();

            $file->move(public_path() . $img_path, $image_name);

            $image_alter = Image::make(sprintf(public_path() . $img_path . '%s', $image_name))->resize(75, 75)->save();

            $user->avatar = $image_name; // Note we add the image path to the databse field before the save.

        }

        return $create;
    } 

4 个答案:

答案 0 :(得分:2)

这是标准的PHP错误消息。您正在静态调用该方法,即从类(Request::hasFile())调用它。 您应该从类Request的实例中调用它。 您可以通过调用服务容器来获取该实例:

protected function create(array $data){
    // ...
    $request = app('request');

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

    // ...

(将Request实例作为控制器方法参数注入可能更自然。当你在框架路由调用的方法中执行它时,这是有效的。我建议另一种方法,因为你自己调用内部方法。)< / p>

答案 1 :(得分:0)

尝试将请求添加到方法

 protected function create(Request $request, array $data){
//code
 }

答案 2 :(得分:0)

您可以使用use Request;代替use Illuminate\Http\Request;

答案 3 :(得分:0)

见下面的代码。它对我有用。

protected function create(array $data)
{
    $request = app('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) );
    } 
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'avatar' => $filename,
        'password' => bcrypt($data['password']),
    ]);
}

请在此处查看详细代码:http://www.pranms.com/how-to-add-avatar-field-in-the-registration-form-in-laravel-5-2/