我在Laravel中使用laravel/passport
进行身份验证。护照的activation_token
列应为随机字符串。
当我在应用程序中注册时,出现此错误:
Illuminate \ Database \ QueryException(HY000)SQLSTATE [HY000]: 常规错误:1364字段“ activation_token”没有默认值 值(SQL:插入
users
(name
,password
,updated_at
,created_at
)值(Alireza,armazochi @ gmail.com, $ 2y $ 10 $ gy2g4uQJPlX / 1HneLEDei.b / BSHrv5B302ifQHPN0G6wbvYnjOiau, 2019-07-13 07:36:18,2019-07-13 07:36:18))
在AuthController.php中,我定义了名称,密码,电子邮件和Activation_token的值。字段名称,电子邮件,密码正确保存在数据库中,但activation_token无法发送到sql查询并创建错误:
SQLSTATE [HY000]:常规错误:1364字段“ activation_token”没有 具有默认值(SQL:插入
users
(name
,password
,updated_at
,created_at
)值(Alireza, armazochi@gmail.com, $ 2y $ 10 $ gy2g4uQJPlX / 1HneLEDei.b / BSHrv5B302ifQHPN0G6wbvYnjOiau, 2019-07-13 07:36:18,2019-07-13 07:36:18))
我将“ activation_token”列更改为$table->string('activation_token')->default(null)->nullable();
它的工作,表明错误与此部分有关。为什么activation_token无法获得随机值?
User.php:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use Notifiable;
use SoftDeletes;
use HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'active', 'activation_token',
];
protected $dates = ['deleted_at'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', //'activation_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
2014_10_12_000000_create_users_table.php:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Str;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('activation_token');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('active')->default(false);
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
AuthController.php:
<?php
namespace App\Http\Controllers;
use App\Notifications\SignupActivate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;
use Illuminate\Support\Str;
class AuthController extends Controller
{
/**
* Create user
*
*
@param [string] name
*
@param [string] email
*
@param [string] password
*
@param [string] password_confirmation
*
@return [string] message
*/
public function signup(Request $request)
{
$request->validate([
'name' => 'required|string',
'email' => 'required|string|email|unique:users',
'password' => 'required|string|confirmed'
]);
$user = new User([
'name' => $request->name,
'activation_token' => Str::random(60),
'email' => $request->email,
'password' => bcrypt($request->password),
]);
$user->save();
$user->notify(new SignupActivate($user));
return response()->json([
'message' => 'Successfully created user!'
], 201);
}
/**
* Login user and create token
*
*
@param [string] email
*
@param [string] password
*
@param [boolean] remember_me
*
@return [string] access_token
*
@return [string] token_type
*
@return [string] expires_at
*/
public function login(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
'remember_me' => 'boolean'
]);
$credentials = request(['email', 'password']);
$credentials['active'] = 1;
$credentials['deleted_at'] = null;
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
$token->expires_at = Carbon::now()->addWeeks(1);
$token->save();
return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString()
]);
}
/**
* Logout user (Revoke the token)
*
*
@return [string] message
*/
public function logout(Request $request)
{
$request->user()->token()->revoke();
return response()->json([
'message' => 'Successfully logged out'
]);
}
/**
* Get the authenticated User
*
*
@return [json] user object
*/
public function user(Request $request)
{
return response()->json($request->user());
}
public function signupActivate($token)
{
$user = User::where('activation_token', $token)->first();
if (!$user) {
return response()->json([
'message' => 'This activation token is invalid.'
], 404);
}
$user->active = true;
$user->activation_token = '';
$user->save();
return $user;
}
}
答案 0 :(得分:0)
这是一个MySQL错误,因为您的数据库字段未设置默认值,并且您没有传递该属性的值。
您可以在表架构设置中设置默认值,例如NULL。
或者,您可以在Laravel中禁用MySQL strict
模式。
/config/database.php
'mysql' => [
...
'strict' => false,
...
],
答案 1 :(得分:0)
在迁移中,您可以执行以下操作以使该列可为空:
$table->string('activation_token')->nullable($value = true);
默认情况下允许 NULL 值插入列