mercredi 15 janvier 2020

Modified user registration in Laravel 5.7 stopped working (Field 'last_name' doesn't have a default value, but data is complete)

I have a Laravel 5.7 app. Users can't register themselves, instead another user has to be logged in to access the registration page. This worked until a few weeks ago (we don't need to register users often), but has stopped working now.

This is the error I get (current_user@example.org is a placeholder for the email address of the user who is currently logged in, not for the new user's address.):

production.ERROR: SQLSTATE[HY000]: General error: 1364 Field 'last_name' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2020-01-15 10:09:52, 2020-01-15 10:09:52)) {"userId":1,"email":"current_user@example.org","exception":"[object] (Illuminate\\Database\\QueryException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'last_name' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2020-01-15 10:09:52, 2020-01-15 10:09:52)) at /html/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'last_name' doesn't have a default value at /html/myproject/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458)
[stacktrace]

This is my RegisterController. I logged $data in the create() function. Everything (including $data['last_name'] seems to be in place:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Log;

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 = '/register';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 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, [
            'first_name' => 'required|max:40',
            'last_name'  => 'required|max:40',
            'role'       => 'required|max:15',
            'email'      => 'required|email|max:255|unique:users',
            'phone'      => 'max:30',
            'password'   => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        // Create flash message
        $flashMessage = '<p>New user registered: ' .
                        $data['first_name'] .
                        ' ' .
                        $data['last_name'] .
                        '</p>';

        // Show flash message on next page view
        session()->flash('flashMessage', $flashMessage);

        return User::create([
            'first_name' => $data['first_name'],
            'last_name'  => $data['last_name'],
            'role'       => $data['role'],
            'email'      => $data['email'],
            'phone'      => $data['phone'],
            'password'   => bcrypt($data['password']),
        ]);
    }
}

This is the RegistersUsers trait:

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use Log;

trait RegistersUsers
{
    use RedirectsUsers;

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    /**
     * 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($this->redirectPath());
    }

    /**
     * Get the guard to be used during registration.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard();
    }

    // registered() is empty
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/36X9IKG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire