作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在默认寄存器表单中遇到 SQLSTATE Integrity Constraint Violation
错误。
我在新表中添加了一个排名行。这是我的迁移;
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('rank');
$table->rememberToken();
$table->timestamps();
});
我希望服务器发送default
的值,所以我将其添加到
\app\Services\Registrar.php
;
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'rank' => 'default',
]);
}
我有以下错误;
Illuminate\Database\QueryException thrown with message "SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.rank (SQL: insert into "users" ("name", "email", "password", "updated_at", "created_at") values (Félix Desjardins, felixinx@gmail.com, y$Rp6YFZivHDqVNsQLDzGar.dEqRf9vxdSdQd/H8.xNWvVLqS0gwpDe, 2015-04-18 15:07:23, 2015-04-18 15:07:23))"
Stacktrace:
#68 Illuminate\Database\QueryException in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:620
#67 Illuminate\Database\Query\Builder:insertGetId in <#unknown>:0
#66 App\Http\Controllers\Auth\AuthController:postRegister in <#unknown>:0
#65 Illuminate\Routing\ControllerDispatcher:Illuminate\Routing\{closure} in <#unknown>:0
#64 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#63 Illuminate\Routing\Router:Illuminate\Routing\{closure} in <#unknown>:0
#62 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#61 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\{closure} in <#unknown>:0
#60 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#59 PDOException in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:358
#58 PDOStatement:execute in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:358
#57 Illuminate\Database\Connection:Illuminate\Database\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:612
#56 Illuminate\Database\Connection:runQueryCallback in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:576
#55 Illuminate\Database\Connection:run in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:359
#54 Illuminate\Database\Connection:statement in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php:316
#53 Illuminate\Database\Connection:insert in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Query\Processors\Processor.php:30
#52 Illuminate\Database\Query\Processors\Processor:processInsertGetId in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php:1718
#51 Illuminate\Database\Query\Builder:insertGetId in <#unknown>:0
#50 call_user_func_array in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:933
#49 Illuminate\Database\Eloquent\Builder:__call in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1608
#48 Illuminate\Database\Eloquent\Builder:insertGetId in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1608
#47 Illuminate\Database\Eloquent\Model:insertAndSetId in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1578
#46 Illuminate\Database\Eloquent\Model:performInsert in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1484
#45 Illuminate\Database\Eloquent\Model:save in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:527
#44 Illuminate\Database\Eloquent\Model:create in C:\xampp\htdocs\omsi-cms\app\Services\Registrar.php:38
#43 App\Services\Registrar:create in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php:50
#42 App\Http\Controllers\Auth\AuthController:postRegister in <#unknown>:0
#41 call_user_func_array in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:246
#40 Illuminate\Routing\Controller:callAction in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:162
#39 Illuminate\Routing\ControllerDispatcher:call in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:107
#38 Illuminate\Routing\ControllerDispatcher:Illuminate\Routing\{closure} in <#unknown>:0
#37 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:141
#36 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\app\Http\Middleware\RedirectIfAuthenticated.php:41
#35 App\Http\Middleware\RedirectIfAuthenticated:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#34 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#33 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:101
#32 Illuminate\Pipeline\Pipeline:then in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:108
#31 Illuminate\Routing\ControllerDispatcher:callWithinStack in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:67
#30 Illuminate\Routing\ControllerDispatcher:dispatch in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Route.php:198
#29 Illuminate\Routing\Route:runWithCustomDispatcher in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Route.php:131
#28 Illuminate\Routing\Route:run in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Router.php:692
#27 Illuminate\Routing\Router:Illuminate\Routing\{closure} in <#unknown>:0
#26 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:141
#25 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#24 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:101
#23 Illuminate\Pipeline\Pipeline:then in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Router.php:694
#22 Illuminate\Routing\Router:runRouteWithinStack in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Router.php:661
#21 Illuminate\Routing\Router:dispatchToRoute in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Routing\Router.php:619
#20 Illuminate\Routing\Router:dispatch in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:214
#19 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\{closure} in <#unknown>:0
#18 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:141
#17 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:43
#16 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in C:\xampp\htdocs\omsi-cms\app\Http\Middleware\VerifyCsrfToken.php:17
#15 App\Http\Middleware\VerifyCsrfToken:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#14 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php:55
#13 Illuminate\View\Middleware\ShareErrorsFromSession:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#12 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php:61
#11 Illuminate\Session\Middleware\StartSession:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#10 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php:36
#9 Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#8 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php:40
#7 Illuminate\Cookie\Middleware\EncryptCookies:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#6 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:42
#5 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:125
#4 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in <#unknown>:0
#3 call_user_func in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:101
#2 Illuminate\Pipeline\Pipeline:then in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:115
#1 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in C:\xampp\htdocs\omsi-cms\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:84
#0 Illuminate\Foundation\Http\Kernel:handle in C:\xampp\htdocs\omsi-cms\public\index.php:53
谢谢!
最佳答案
为了保护您免受批量分配漏洞的影响,Laravel 要求您显式设置(或排除)您想要的值 mass assignable通过 create()
方法传递它们。
因此,如果您希望 rank
成为可质量分配的属性,则必须将其添加到 $fillable
属性(这只是质量的数组) -可分配的属性)在您的用户模型上。可以说,不在“白名单”上的任何其他属性都将被忽略。因此,在您的用户模型上,您只需执行以下操作:
protected $fillable = [
'name',
'email',
'password',
'rank',
];
您必须对每个想要批量分配的模型类执行此操作。
专业提示:您可以在架构中分配默认值,因此不必每次创建用户时都传递它们:
$table->string('rank')->default('default');
如果您在创建时没有传递特定的“等级”,它将回退到“默认”。
另一个选项(应谨慎使用)是使用 forceCreate
显式忽略批量分配约束:
User::forceCreate([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'rank' => 'default',
]);
关于php - 拉拉维尔 : Integrity constraint violation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719139/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!