I'm 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`
The register controller:
<?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;
}
}
Try moving $request to first position:
protected function create(Request $request, array $data)
{
.....
PS: don't work if return the user created... or is only test?, xD
Add a new __contruct
method as follows:
protected $request;
public function __contruct(Request $request)
{
$this->request = $request;
}
And in your create method:
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;
}