Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created October 16, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogcacanid/8e7a606f419b076a870d8f5888252452 to your computer and use it in GitHub Desktop.
Save blogcacanid/8e7a606f419b076a870d8f5888252452 to your computer and use it in GitHub Desktop.
UserModel.php Login Dan Register System CodeIgniter 4
<?php namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $allowedFields = ['name', 'email', 'password'];
protected $useTimeStamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $validationRules = [
'name' => 'required',
'email' => 'required|valid_email|is_unique[users.email]',
'password' => 'required|min_length[8]'
];
protected $validationMessages = [
'email' => [
'is_unique' => 'Sorry, That email has already been taken. Please choose another.'
]
];
protected $skipValidation = false;
protected $beforeInsert = ['hashPassword'];
protected function hashPassword(array $data)
{
if (! isset($data['data']['password'])) {
return $data;
}
$data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment