我正在Laravel上的新项目上注册并收到错误
SQLSTATE [HY000]:常规错误:1364字段'登录'没有默认值(SQL:插入users
()values())
这是我的代码。
视图:
@extends('app')
@section('content')
<h1>Регистрация</h1>
<form action="/user/" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="login">Login</label>
<input type="text" class="form-control" name="login">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<label for="password-repeat">Repeat password</label>
<input type="password" class="form-control" id="password-repeat" name="password-repeat">
</div>
<button type="submit" class="btn btn-default">Регистрация</button>
</form>
@stop
控制器:
public function create()
{
$userConstuctor['login'] = Request::input('login');
$userConstuctor['password'] = Request::input('password');
$user = new User($userConstuctor);
$user->setUser(Request::all());
$user->save();
}
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Session;
class User extends Model
{
const ADMIN = 1;
const MANAGER = 2;
const USER = 3;
public $login;
public $password;
private $email;
private $firstName;
private $secondName;
private $lastName;
private $avatar;
private $userOptions = [];
private $createdDate;
private $rememberToken;
private $active;
private $groupId;
public $timestamps = false;
protected $fillable = ['login', 'password', 'email', 'created_date', 'remember_token', 'active', 'group_id'];
protected $table = 'users';
public function __construct($userConstuctor)
{
$this->setLogin($userConstuctor['login']);
$this->setPassword($userConstuctor['password']);
}
public function setUser($user)
{
$this->setEmail($user['email']);
$this->setCreatedDate();
$this->setRememberToken($user['_token']);
$this->setActive();
$this->setGroupId();
}
private function setLogin($login)
{
$this->login = $login;
}
private function setPassword($password)
{
$this->password = md5($password);
}
private function setEmail($email)
{
$this->email = $email;
}
private function setCreatedDate()
{
$this->createdDate = date('Y-m-d H:i:s');
}
private function setRememberToken($token)
{
$this->rememberToken = $token;
}
private function setActive()
{
$this->active = false;
}
private function setGroupId()
{
$this->groupId = self::USER;
}
}
表架构:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('login')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('first_name')->nullable();
$table->string('second_name')->nullable();
$table->string('last_name')->nullable();
$table->string('avatar', 250)->nullable();
$table->longText('user_options')->nullable();
$table->timestamp('created_date');
$table->boolean('active');
$table->rememberToken('token');
$table->integer('group_id');
});
我从dd()
得到的回复: -
http://joxi.net/823WMQ4f647LX2
答案 0 :(得分:0)
您收到此错误,因为您的查询无法在表中插入任何内容 另外,我需要知道你需要从多个控制器创建用户吗?
试试这个
public function create(Request $request)
{
$userConstuctor['login'] = $request->login;
$userConstuctor['password'] = $request->password;
$user = new User();
$user->login = $request->login;
$user->password = $request->password;
/*and so on....*/
$user->save();
}