Hi everybody.
I'm learning Laravel and I've got an issue with the Auth code. The issue is that Laravel seems to pretend that to register a new user all registration form fields be filled, even the not required ones.
This is my RegisterController.php
<?phpnamespace App\Http\Controllers\Auth;use App\Http\Controllers\Controller;use App\Providers\RouteServiceProvider;use App\User;use Illuminate\Foundation\Auth\RegistersUsers;use Illuminate\Support\Facades\Hash;use Illuminate\Support\Facades\Validator;use Illuminate\Http\Request;use Illuminate\Auth\Events\Registered;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 registration. * * @var string */ protected $redirectTo = RouteServiceProvider::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', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], 'company' => ['string'], 'website' => ['string'], 'phone' => ['numeric'], 'mobile' => ['numeric'], 'type' => ['string'], ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), //using data_get doesn't change anything 'company' => data_get( $data, 'company'), 'website' => data_get($data, 'website'), 'phone' => data_get($data, 'phone'), 'mobile' => data_get($data, 'mobile'), 'type' => data_get($data, 'type'), // 'company' => $data['company'], // 'website' => $data['website'], // 'phone' => $data['phone'], // 'mobile' => $data['mobile'], // 'type' => $data['type'], ]); } /** * https://laraveldaily.com/auth-after-registration-redirect-to-previous-intended-page/ * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function register(Request $request) { $this->validator($request->all())->validate(); event(new Registered($user = $this->create($request->all()))); $this->guard()->login($user); return $this->registered($request, $user) ?: redirect()->intended($this->redirectPath()); }}
I thought that leaving added columns empty whould have worked fine but I've got validation errors like "The website must be a string", so I googled a bit and I have created a new migration:
<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class AddDefaultNullToSomeColumn extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('company')->nullable()->default(null)->change(); $table->string('website')->nullable()->default(null)->change(); $table->string('phone')->nullable()->default(null)->change(); $table->string('mobile')->nullable()->default(null)->change(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->string('company')->nullable()->change(); $table->string('website')->nullable()->change(); $table->string('phone')->nullable()->change(); $table->string('mobile')->nullable()->change(); }); }}
Actually, the db table allows those fields to be NULL and their default value is NULL but I still get those errors and to successfully register a new user I must fill all fields in the form.
What I am missing here?
Hi Chris, thank you for your answer.
I'm using Laravel 6.0.
I thought that just declaring fields as nullable in the migration were enough :)
Going to try your suggestion.
Marco Gasi
ASKER
Thank you, Chris, it works like a charm.
Chris Stanyon
No worries Marco,
Glad I could help.
FYI - the Validations in your controller, aren't directly related to the tables (although it will help to prevent invalid data being saved to your DB)
the Validations in your controller, aren't directly related to the tables (although it will help to prevent invalid data being saved to your DB)
I'm not sure to understand what you mean.... Actually, no, I'm absolutely sure I don't :)
Chris Stanyon
Hey Marco,
In your comments you said that the DB columns allow null but you still get the error.
My point was that you're dealing with 2 separate issues here - when you post a form to a controller, you validate the data against a set of rules. Your data either passes or fails the validation. This has nothing to do with your Database, the Tables or the Columns - you're simply validating the data a user has submitted.
After the data has been validated, you then store that data into the database - as a separate process.
That's why I said that the Validations aren't related to your Tables.
Marco Gasi
ASKER
Okay, thank you so much for the additional input, very clear. Ciao.
I'm using Laravel 6.0.
I thought that just declaring fields as nullable in the migration were enough :)
Going to try your suggestion.