即时通讯在流明中使用jwt,每当我尝试创建新用户时,我都会收到错误消息
SQLSTATE [42S22]:未找到列:1054未知列' users.id'在 ' where where' (SQL:select * from users,其中users.id为空限制 1)
虽然我的数据库或控制器中没有users.id
这是我的控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
class UserController extends Controller
{
/**
* Register new user
*
* @param $request Request
*/
public function register(Request $request)
{
$hasher = app()->make('hash');
$random = strtoupper(md5(microtime(8)));
$username = $request->input('username');
$email = $request->input('email');
$password = $hasher->make($request->input('password'));
//
$users = User::all();
foreach($users as $user){
if($user->username == $username){
$res['success'] = false;
$res['message'] = 'Username Unavailable!';
return response($res);
}
if($user->email == $email){
$res['success'] = false;
$res['message'] = 'Email Unavailable!';
return response($res);
}
}
//
$register = User::create([
'user_id' => $random,
'username'=> $username,
'email'=> $email,
'password'=> $password,
]);
if ($register) {
$res['success'] = true;
$res['message'] = 'Register Success!';
return response($res);
}
else{
$res['success'] = false;
$res['message'] = 'Failed to register!';
return response($res);
}
}
}
数据库:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->string('user_id', 8)->primary();
$table->string('username', 32);
$table->string('email', 250)->unique();
$table->string('password');
$table->string('api_token');
$table->tinyInteger('state');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
型号:
namespace App;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements JWTSubject, AuthenticatableContract,
AuthorizableContract
{
use Authenticatable, Authorizable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id', 'username', 'email', 'api_token', 'password'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password'
];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}
谁能告诉我做错了什么?
答案 0 :(得分:0)
代替此$ table-&gt;字符串(&#39; user_id&#39;,8) - &gt; primary(); 尝试$ table-&gt;增量(&#39; user_id&#39;); 你尝试将主键作为字符串更好的主要是整数