Laravel:完整性约束违规

时间:2015-04-18 15:13:46

标签: php sqlite laravel

我的默认注册表格出现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

谢谢!

1 个答案:

答案 0 :(得分:8)

为了保护您免受质量分配漏洞的影响,Laravel要求您通过create() - 方法传递它们,以明确设置(或排除)您希望成为mass assignable的值。

因此,如果您希望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',
    ]);