8000 fix 新增通用Cast处理对象 · laraveladmin-cn/laraveladmin@eccf114 · GitHub
[go: up one dir, main page]

Skip to content

Commit eccf114

Browse files
author
张世平
committed
fix 新增通用Cast处理对象
1 parent 2fefe5c commit eccf114

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

app/Models/Casts/BitCheckCast.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Models\Casts;
4+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
5+
6+
/**
7+
* 二进制位表示多选选项
8+
*/
9+
class BitCheckCast implements CastsAttributes
10+
{
11+
public function get($model, string $key, $value, array $attributes)
12+
{
13+
$field = $model->getFieldsMap($key)->toArray();
14+
unset($field[0]);
15+
return multiple($value,$field);
16+
}
17+
18+
public function set($model, string $key, $value, array $attributes)
19+
{
20+
return multipleToNum($value);
21+
}
22+
}

app/Models/Casts/RateCast.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models\Casts;
4+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
5+
6+
/**
7+
* 佣金率值放大缩小处理
8+
*/
9+
class RateCast implements CastsAttributes
10+
{
11+
public function get($model, string $key, $value, array $attributes)
12+
{
13+
return $value/10000;
14+
}
15+
16+
public function set($model, string $key, $value, array $attributes)
17+
{
18+
return intval($value*10000);
19+
}
20+
}

app/Models/Casts/StrSplitCast.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models\Casts;
4+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
5+
6+
/**
7+
* 字符串拆解
8+
*/
9+
class StrSplitCast implements CastsAttributes
10+
{
11+
public function get($model, string $key, $value, array $attributes)
12+
{
13+
return $value?explode(',',$value):[];
14+
}
15+
16+
public function set($model, string $key, $value, array $attributes)
17+
{
18+
return $value?implode(',',$value):'';
19+
}
20+
}

0 commit comments

Comments
 (0)
0