我在背包里为我的用户做了一个crud。当我创建用户时,一切正常,但如果我更新,我会收到“密码”错误,因为如果我不想更新它,我会将其留空,密码不能为空。
如果我将其留空,如何才能不在 POST 中添加“密码”?或者我如何默认当前密码?还有一个“确认”字段?
在 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)
最佳答案
如果有人正在为 Backpack 制作自己的 UserCrudController,请查看他们为权限管理器所做的 Controller :
关于php - laravel背包用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608870/