sumonmselim's avatar

Laravel built in auth with user image

Im using laravel built in auth system that can be found easily with running php artisan make:auth command in a new laravel project. While registering i would like to give the user, the ability to upload their profile picture too. So i have added a new field called profile_picture in the registration form. How do i receive the file from backend? I have tried adding the Request $request argument to the create method after array $data But it says-

Type error: Argument 1 passed to App\Http\Controllers\Auth\RegisterController::create() must be an instance of Illuminate\Http\Request, none given, called in /home/tawsif/laravel/repto_events/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 33

====== 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 { 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 likes
6 replies
Sys32's avatar

Its generally best to have the user register and then upload an image after signing up.

This is because if the validation of the image fails, size, resolution, ext etc. Its gonna take them a few tries to get right.

So what I would do is set a default image, and then have them upload one on their manage account/edit account view.

sumonmselim's avatar

yes i know that. But Im in a situation where my boss wants me to take the user profile image while registering.

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

}

popcone's avatar

@sumonselim while pasting your code here, please include it in three back ticks (`) so that others can read it easily

popcone's avatar

You do not need to pass $request to create method.

$data is actually $request->all(). So you can get all request from $data

Or if you want to override register method. Include below code in your controller and modify it.

public function register(Request $request)
{
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return redirect($this->redirectPath());
}
1 like
WebKenth's avatar

Make an Ajax call sending the file to the server, validating and so on.

Then when the user registers, attach the image to your User model. If the user doesn't register within an hour, delete the file

Please or to participate in this conversation.