2017-10-06 65 views
0

我爲揹包裏的用戶做了一個粗糙的東西。當我創建一個用戶時,一切正常,但如果我更新,我得到一個「密碼」的錯誤,因爲如果我不想更新它,我將它留空,並且密碼不能爲空。laravel揹包用戶密碼

如果我把它留給空白,我怎麼能不把「密碼」添加到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) 
+0

讓我看看你的控制器的方法和形式 – iCoders

+0

@iCoders我加了控制器。我又一次使用Backpack作爲我的CRUD。 https://laravel-backpack.readme.io/docs – Packy

+0

你可以dd($ request-> all());並顯示輸出並打印Auth :: user() – iCoders

回答

0

你可以嘗試刪除像這樣的密碼:

  // Hash password before save 
     if (!empty($request->password)) { 
      $request->offsetSet('password', Hash::make($request->password)); 
     }else{ 
      $request->remove('password'); 
     }