laravel背包用户密码 [英] laravel backpack user password
问题描述
我在背包里为我的用户们赚了一笔.创建用户时,一切正常,但是如果更新,则会出现密码"错误,因为如果我不想更新它,则将其保留为空白,密码不能为null.
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.
如果我将其保留为空白,如何不将密码"添加到POST?或如何默认使用当前密码?并有一个确认"字段?
How can I not add "password" to the POST if I leave it blank? Or how do I default to the current password?? and also have a "confirm" field?
在UserCrudController.php中
In 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();
}
}
错误:
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)
推荐答案
如果有人正在为背包制作自己的UserCrudController,请查看他们的控制器,他们是为权限管理器做的:
If anyone is making their own UserCrudController for Backpack look at their controller they did for a permissions manager:
这篇关于laravel背包用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!