正在显示
1 个修改的文件
包含
21 行增加
和
0 行删除
| @@ -4,6 +4,7 @@ namespace App\Models; | @@ -4,6 +4,7 @@ namespace App\Models; | ||
| 4 | 4 | ||
| 5 | use Illuminate\Database\Eloquent\Model; | 5 | use Illuminate\Database\Eloquent\Model; |
| 6 | use Illuminate\Support\Facades\DB; | 6 | use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Schema; | ||
| 7 | 8 | ||
| 8 | class Base extends Model | 9 | class Base extends Model |
| 9 | { | 10 | { |
| @@ -94,6 +95,7 @@ class Base extends Model | @@ -94,6 +95,7 @@ class Base extends Model | ||
| 94 | * @time :2023/8/21 17:18 | 95 | * @time :2023/8/21 17:18 |
| 95 | */ | 96 | */ |
| 96 | public function add($data){ | 97 | public function add($data){ |
| 98 | + $data = $this->filterRequestData($data,$this->table); | ||
| 97 | $data['created_at'] = date('Y-m-d H:i:s'); | 99 | $data['created_at'] = date('Y-m-d H:i:s'); |
| 98 | $data['updated_at'] = $data['created_at']; | 100 | $data['updated_at'] = $data['created_at']; |
| 99 | return $this->insert($data); | 101 | return $this->insert($data); |
| @@ -107,6 +109,7 @@ class Base extends Model | @@ -107,6 +109,7 @@ class Base extends Model | ||
| 107 | * @time :2023/8/21 17:17 | 109 | * @time :2023/8/21 17:17 |
| 108 | */ | 110 | */ |
| 109 | public function addReturnId($data){ | 111 | public function addReturnId($data){ |
| 112 | + $data = $this->filterRequestData($data,$this->table); | ||
| 110 | $data['created_at'] = date('Y-m-d H:i:s'); | 113 | $data['created_at'] = date('Y-m-d H:i:s'); |
| 111 | $data['updated_at'] = $data['created_at']; | 114 | $data['updated_at'] = $data['created_at']; |
| 112 | return $this->insertGetId($data); | 115 | return $this->insertGetId($data); |
| @@ -123,6 +126,7 @@ class Base extends Model | @@ -123,6 +126,7 @@ class Base extends Model | ||
| 123 | if(isset($data['id']) && !empty($data['id'])){ | 126 | if(isset($data['id']) && !empty($data['id'])){ |
| 124 | unset($data['id']); | 127 | unset($data['id']); |
| 125 | } | 128 | } |
| 129 | + $data = $this->filterRequestData($data,$this->table); | ||
| 126 | $query = $this->formatQuery($condition); | 130 | $query = $this->formatQuery($condition); |
| 127 | $data['updated_at'] = date('Y-m-d H:i:s'); | 131 | $data['updated_at'] = date('Y-m-d H:i:s'); |
| 128 | return $query->update($data); | 132 | return $query->update($data); |
| @@ -251,4 +255,21 @@ class Base extends Model | @@ -251,4 +255,21 @@ class Base extends Model | ||
| 251 | return true; | 255 | return true; |
| 252 | } | 256 | } |
| 253 | 257 | ||
| 258 | + /** | ||
| 259 | + * @remark :过滤掉请求数据中不存在于数据库表中的字段 | ||
| 260 | + * @name :filterRequestData | ||
| 261 | + * @author :lyh | ||
| 262 | + * @method :post | ||
| 263 | + * @time :2024/6/14 10:49 | ||
| 264 | + */ | ||
| 265 | + public function filterRequestData(array $data, $table) | ||
| 266 | + { | ||
| 267 | + // 获取表的字段列表 | ||
| 268 | + $columns = Schema::getColumnListing($table); | ||
| 269 | + // 过滤数据 | ||
| 270 | + return array_filter($data, function ($key) use ($columns) { | ||
| 271 | + return in_array($key, $columns); | ||
| 272 | + }, ARRAY_FILTER_USE_KEY); | ||
| 273 | + } | ||
| 274 | + | ||
| 254 | } | 275 | } |
-
请 注册 或 登录 后发表评论