У меня есть ошибка 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 требует, чтобы вы явно задавали (или исключали) значения, которые вы хотите назначить массе , передавая их через метод create()
.
Поэтому, если вы хотите, чтобы rank
был атрибутом, назначаемым массой, вам нужно добавить его в $fillable
-property (который является всего лишь массивом атрибутов, присваиваемых массой) в вашей пользовательской модели. Любой другой атрибут, который не включен в «белый список», так сказать, будет проигнорирован. Так что на вашей модели пользователя вы просто делаете:
protected $fillable = [ 'name', 'email', 'password', 'rank', ];
Вы должны сделать это для каждого модельного класса, который вы хотите назначить по массе.
Pro-Tip: вы можете назначать значения по умолчанию в своей схеме, поэтому вам не нужно передавать их каждый раз, когда вы создаете пользователя:
$table->string('rank')->default('default');
Если вы не пройдете определенный «рейтинг» во время создания, он вернется к «умолчанию».
Другой вариант (который следует использовать с осторожностью) заключается в том, чтобы явно игнорировать ограничения массового присвоения с помощью forceCreate
:
User::forceCreate([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), 'rank' => 'default', ]);