Laravel内置了用户映像



我使用的是Laravel内置的认证系统,可以在新的Laravel项目中运行php artisan make:auth命令轻松找到。在注册时,我想给用户,上传他们的个人资料图片的能力。

所以我在注册表单中添加了一个名为profile_picture的新字段。我如何从后端接收文件?我已经尝试将Request $request参数添加到数组$data之后的创建方法,但它说:

类型错误:参数1传递给AppHttpControllersAuthRegisterController::create()必须是一个实例IlluminateHttpRequest, none given,在/home/tawsifv/laravel/repto_events/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php第33行

寄存器控制器:

<?php
namespace AppHttpControllersAuth;
use AppUser;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthRegistersUsers;
use IlluminateHttpRequest;

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 IlluminateContractsValidationValidator
     */
    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;
    }
}

尝试将$request移到第一个位置:

protected function create(Request $request, array $data)
    {
    .....

PS:不工作,如果返回用户创建…还是仅仅是测试?div, xD

新增__contruct方法如下:

protected $request;
public function __contruct(Request $request)
{
    $this->request = $request;
}

在你的create方法中:

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;
    }

相关内容

  • 没有找到相关文章

最新更新