ldc-crm/app/Models/CustomerLogin.php
2025-07-28 09:07:54 +02:00

107 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
/**
* @property integer $id Id.
* @property integer $customer_id Foreign key Customer.
* @property integer $brand_id Foreign key Brand.
* @property string $password Answer.
* @property \Carbon\Carbon $last_connection Answer.
* @property boolean $status Answer.
* @property \Illuminate\Support\Carbon $created_at Created date.
* @property \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Customer $customer Customer BelongsTo relation.
* @property-read \App\Models\Brand $brand Brand BelongsTo relation.
*/
class CustomerLogin extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'password',
];
/**
* The model's attributes.
*
* @var array<string, mixed>
*/
protected $attributes = [
'status' => false,
];
/**
* The attributes that should be cast to native types.
*
* @var array<string, mixed>
*/
protected $casts = [
'status' => 'bool',
'last_connection' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
if (Hash::needsRehash($model->password)) {
$model->password = Hash::make(trim($model->password));
}
});
static::updating(function (self $model) {
if (Hash::needsRehash($model->password)) {
$model->password = Hash::make(trim($model->password));
}
});
}
// * RELATIONS
/**
* Customer belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Brand belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Brand::class);
}
// * METHODS
/**
* Check if the user password is correct.
*
* @param string $password
* @return boolean
*/
public function passwordMatch(string $password): bool
{
return Hash::check($password, $this->password);
}
}