[英]laravel backpack user password
I made a crud for my users in backpack. 我在背包里为我的用户们赚了一笔。 When I create a user everything works fine but if I update I get an error for "Password" since if I dont want to update it I leave it blank and password cannot be null. 创建用户时,一切正常,但如果更新,则会出现“密码”错误,因为如果我不想更新它,则将其保留为空白,密码不能为null。
How can I not add "password" to the POST if I leave it blank? 如果我将“ password”保留为空白,如何不将其添加到POST? Or how do I default to the current password?? 或者如何默认使用当前密码? and also have a "confirm" field? 并有一个“确认”字段?
In UserCrudController.php 在UserCrudController.php中
<?php
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use App\User;
use Auth;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\UserCrudRequest as StoreRequest;
use App\Http\Requests\UserCrudRequest as UpdateRequest;
class UserCrudController extends CrudController {
public function setup() {
$this->crud->setModel("App\User");
$this->crud->setRoute(config('backpack.base.route_prefix')."/user");
$this->crud->setEntityNameStrings('user', 'users');
$this->crud->setColumns([
['name' => 'lastname', 'label' => "Last Name"],
['name' => 'firstname', 'label' => "First Name"],
]);
$this->crud->addFields([
[
'name' => 'firstname',
'label' => "First name"
],
[
'name' => 'lastname',
'label' => "First name"
],
[ // Password
'name' => 'password',
'label' => 'Password',
'type' => 'password'
],
]);
}
public function store(StoreRequest $request)
{
// <--------- here is where a before_insert callback logic would be
$response = parent::storeCrud();
// <--------- here is where a after_insert callback logic would be
return $response;
}
public function update(UpdateRequest $request)
{
$user = User::find(Auth::user()->id);
// Hash password before save
if (!empty($request->password)) {
$request->offsetSet('password', Hash::make($request->password));
}else{
$request->offsetSet('password', $user->password );
}
return parent::updateCrud();
}
}
Error: 错误:
Integrity constraint violation: 1048 Column 'password' cannot be null (SQL: update `users` set `password` = , `updated_at` = 2017-10-06 15:08:25 where `id` = 1)
If anyone is making their own UserCrudController for Backpack look at their controller they did for a permissions manager: 如果有人正在为背包制作自己的UserCrudController,请查看他们的控制器,他们是为权限管理器做的:
https://github.com/Laravel-Backpack/PermissionManager/blob/master/src/app/Http/Controllers/UserCrudController.php https://github.com/Laravel-Backpack/PermissionManager/blob/master/src/app/Http/Controllers/UserCrudController.php
you can try remove password like this : 您可以尝试删除密码,如下所示:
// Hash password before save
if (!empty($request->password)) {
$request->offsetSet('password', Hash::make($request->password));
}else{
$request->remove('password');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.