Merge branch 'develop'
This commit is contained in:
@@ -46,4 +46,31 @@ class CrmCustomer extends Model
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::saved(function ($customer) {
|
||||
if (empty($customer->code)) {
|
||||
$customer->code = self::generateCustomerCode($customer);
|
||||
// 使用saveQuietly避免递归调用
|
||||
$customer->saveQuietly();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function generateCustomerCode($customer)
|
||||
{
|
||||
// 生成唯一16位code,CM+14位
|
||||
$unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 8, 6));
|
||||
$code = 'CM' . date('Ymd') .$unique;
|
||||
return $code;
|
||||
}
|
||||
|
||||
// 静默保存方法,不触发事件
|
||||
public function saveQuietly(array $options = [])
|
||||
{
|
||||
return static::withoutEvents(function () use ($options) {
|
||||
return $this->save($options);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user