80 lines
2.0 KiB
PHP
Executable File
80 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\AffiliatesAdmin\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Affiliate extends Model
|
|
{
|
|
protected $table = 'affiliates';
|
|
|
|
protected $primaryKey = 'affiliates_id';
|
|
|
|
protected $fillable = [
|
|
'affiliates_code',
|
|
'affiliates_name',
|
|
'affiliates_email',
|
|
'affiliates_phone',
|
|
'affiliates_address',
|
|
'affiliates_website',
|
|
'affiliates_commission_rate',
|
|
'affiliates_total_earned',
|
|
'affiliates_total_paid',
|
|
'affiliates_status',
|
|
];
|
|
|
|
/**
|
|
* The "booting" method of the model.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
// Ensure affiliates_code is generated before creating
|
|
static::creating(function ($affiliate) {
|
|
if (empty($affiliate->affiliates_code)) {
|
|
$affiliate->affiliates_code = static::generateUniqueAffiliateCode();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Generate a unique affiliate code.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected static function generateUniqueAffiliateCode(): string
|
|
{
|
|
$code = \Illuminate\Support\Str::random(10); // Using Laravel's Str helper
|
|
while (static::where('affiliates_code', $code)->exists()) {
|
|
$code = \Illuminate\Support\Str::random(10);
|
|
}
|
|
return $code;
|
|
}
|
|
|
|
/**
|
|
* Get the commissions for the affiliate.
|
|
*/
|
|
public function commissions()
|
|
{
|
|
return $this->hasMany(AffiliateCommission::class, 'affiliate_commissions_affiliate_id', 'affiliates_id');
|
|
}
|
|
|
|
/**
|
|
* Get the banners created by the affiliate.
|
|
*/
|
|
public function banners()
|
|
{
|
|
return $this->hasMany(AffiliateBanner::class, 'affiliate_banners_affiliate_id', 'affiliates_id');
|
|
}
|
|
|
|
/**
|
|
* Get the clicks for the affiliate's banners.
|
|
*/
|
|
public function clicks()
|
|
{
|
|
return $this->hasMany(AffiliateClick::class, 'affiliate_clicks_affiliate_id', 'affiliates_id');
|
|
}
|
|
} |