Reputation:
Im using laravel auth. In the register page the user needs to fill in the name, surname, email and password to create an account. Later the user can edit his profile account and insert more info like address, city, zipcode, etc, but to create account its just necessary name, surname, email and password.
Im structuring this above logic with the code below using the laravel auth, but Im getting an error after filling the form and click in "Create Account":
SQLSTATE[HY000]: General error: 1364 Field 'facebook_id' doesn't have a default value (SQL: insert into users
(name
, email
, password
, updated_at
, created_at
) values (...)
Do you know what is the error in the code below?
Routes:
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
User table migration:
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('facebook_id')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('phone');
$table->dateTime('dateRegister');
$table->string('address');
$table->string('zipCode');
$table->string('city');
$table->timestamps();
});
}
}
User model:
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'facebook_id', ''
];
protected $hidden = [
'password',
];
}
RegisterController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
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:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Upvotes: 2
Views: 183
Reputation: 44
If you are not providing the facebook_id
to create
method then make it nullable in migration file
$table->string('facebook_id')->unique()->nullable();
Upvotes: 0
Reputation: 7489
You need either pass the field to create()
method with value or make it nullable
in migration
$table->string('facebook_id')->unique()->nullable();
Upvotes: 0
Reputation: 9853
Change your migration
and allow facebook_id
to be null, otherwise provide facebook_id
when inserting
:
$table->string('facebook_id')->unique()->nullable();
Upvotes: 1
Reputation: 163758
If during registration you're passing Facebook ID, add it:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'facebook_id' => $data['facebook_id']
]);
If not, make this column nullable:
$table->string('facebook_id')->unique()->nullable();
Upvotes: 2