Lae/app/Models/Admin/Admin.php

44 lines
900 B
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Models\Admin;
2022-08-12 08:10:45 +00:00
use Illuminate\Support\Str;
2022-09-08 16:27:05 +00:00
use Illuminate\Support\Facades\Crypt;
2022-08-12 07:56:56 +00:00
use Illuminate\Database\Eloquent\Model;
2022-08-12 08:10:45 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2022-08-12 07:56:56 +00:00
class Admin extends Model
{
2022-08-12 08:10:45 +00:00
use HasFactory, SoftDeletes;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password'
];
// protect password
public function setPasswordAttribute($value)
{
2022-09-08 16:27:05 +00:00
$this->attributes['password'] = Crypt::encrypt($value);
2022-08-12 08:10:45 +00:00
}
// before create admin, generate api_token
public static function boot()
{
parent::boot();
self::creating(function ($admin) {
2022-08-13 08:37:17 +00:00
2022-08-16 06:50:18 +00:00
// if not set api_token
if (!$admin->api_token) {
2022-08-13 08:37:17 +00:00
$admin->api_token = Str::random(60);
}
2022-09-08 16:27:05 +00:00
2022-08-12 08:10:45 +00:00
});
}
2022-08-12 07:56:56 +00:00
}