У меня есть эта проблема из нескольких дней в Scrutinizer CI.
Мне бы это удалось, но я не знаю, как это сделать. Это скрипт миграции:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('password');
$table->rememberToken();
$table->datetime('last_change_password')->nullable();
$table->timestamps();
});
}
А это мой App\User
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Validator;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* @param int $id the id of the user
* @param array $data
*
* @return mixed
*/
public function changePassword(int $id, array $data)
{
$rules = [
'username' => 'sometimes|required|string|min:8|unique:users,username,'.$id,
'new_password' => 'required|string|confirmed|min:8'
];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
return $validator->errors()->all();
} else {
$object = $this->find($id);
if (is_object($object)) {
if (isset($data[ 'username' ])) {
$object->username = $data[ 'username' ];
}
$object->password = bcrypt($data[ 'new_password' ]);
$object->last_change_password = \Carbon\Carbon::now();
$status = $object->save();
} else {
$status = false;
}
return $status;
}
}
/**
*
* @param array $data the data to update
* @return bool
*/
public function updateRecord(array $data)
{
$rules = [
'username' => 'sometimes|required|string|min:8|unique:users,username,'.$data[ 'id' ],
'password' => 'required|string|min:8'
];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
return $validator->errors()->all();
} else {
$id = $data[ 'id' ];
$object = $this->find($id);
if (is_object($object)) {
foreach ($data as $key => $value) {
if (in_array($key, $this->fillable)) {
$object->$key = $value;
}
}
$status = $object->save();
} else {
$status = false;
}
return $status;
}
}
}