我現在有一個表單,它工作得很好。基本上我是添加用戶,並根據表單分配角色。大。但是現在我正試圖在「avatar」列下的用戶表中添加一個頭像。我發現有道理,但沒有工作,得到錯誤「非靜態方法照亮\ HTTP \支持:: hasFile()不應該被靜態地稱爲」Laravel在用戶註冊中添加頭像
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;
}
由你有這條線的兩倍的方式,它的無用我想既然你可以使用相同的用戶$'$ USER =用戶::發現($ create-> id);' –