我使用Laravel内置的auth系统,可以在新的Laravel项目中运行php artisan make:auth
命令轻松找到。注册时我想给用户,也可以上传他们的个人资料图片。
所以我在注册表单中添加了一个名为profile_picture的新字段。如何从后端接收文件?我尝试在数组$ data之后将Request $request
参数添加到create方法,但是它说:
类型错误:传递给App \ Http \ Controllers \ Auth \ RegisterController :: create()的参数1必须是Illuminate \ Http \ Request的实例,没有给出,在/ home / tawsif / laravel / repto_events / vendor中调用第33行/ laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php
寄存器控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'profession' => 'required|min:3',
'phone' => 'required|digits:11',
'address' => 'required|min:5'
// 'profile_picture' => 'required|image'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(Request $request, array $data )
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'ssc_year' => $data['ssc'],
'hsc_year' => $data['hsc'],
'profession' => $data['profession'],
'address' => $data['address'],
]);
$file = $request->file('profile_picture');
$thumbnail_path = public_path('uploads/propic/thumbnail/');
$original_path = public_path('uploads/propic/original/');
$file_name = 'user_'. $user->id .'_'. str_rand(32) . '.' . $file->getClientOriginalExtension();
Image::make($file)
->resize(261,null,function ($constraint) {
$constraint->aspectRatio();
})
->save($original_path . $file_name)
->resize(90, 90)
->save($thumbnail_path . $file_name);
$user->update(['profile_picture' => $file_name]);
return $user;
}
}
答案 0 :(得分:0)
尝试将$ request移至第一个位置:
protected function create(Request $request, array $data)
{
.....
PS:如果返回用户创建的...或者只是测试?不能正常工作?,xD
答案 1 :(得分:0)
添加新的__contruct
方法,如下所示:
protected $request;
public function __contruct(Request $request)
{
$this->request = $request;
}
在你的创建方法中:
protected function create(Request $request, array $data )
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'phone' => $data['phone'],
'ssc_year' => $data['ssc'],
'hsc_year' => $data['hsc'],
'profession' => $data['profession'],
'address' => $data['address'],
]);
$file = $this->request->file('profile_picture');
$thumbnail_path = public_path('uploads/propic/thumbnail/');
$original_path = public_path('uploads/propic/original/');
$file_name = 'user_'. $user->id .'_'. str_rand(32) . '.' . $file->getClientOriginalExtension();
Image::make($file)
->resize(261,null,function ($constraint) {
$constraint->aspectRatio();
})
->save($original_path . $file_name)
->resize(90, 90)
->save($thumbnail_path . $file_name);
$user->update(['profile_picture' => $file_name]);
return $user;
}