|
...
|
...
|
@@ -4,6 +4,7 @@ namespace App\Models; |
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
|
|
|
|
class Base extends Model
|
|
|
|
{
|
|
...
|
...
|
@@ -94,6 +95,7 @@ class Base extends Model |
|
|
|
* @time :2023/8/21 17:18
|
|
|
|
*/
|
|
|
|
public function add($data){
|
|
|
|
$data = $this->filterRequestData($data,$this->table);
|
|
|
|
$data['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$data['updated_at'] = $data['created_at'];
|
|
|
|
return $this->insert($data);
|
|
...
|
...
|
@@ -107,6 +109,7 @@ class Base extends Model |
|
|
|
* @time :2023/8/21 17:17
|
|
|
|
*/
|
|
|
|
public function addReturnId($data){
|
|
|
|
$data = $this->filterRequestData($data,$this->table);
|
|
|
|
$data['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$data['updated_at'] = $data['created_at'];
|
|
|
|
return $this->insertGetId($data);
|
|
...
|
...
|
@@ -123,6 +126,7 @@ class Base extends Model |
|
|
|
if(isset($data['id']) && !empty($data['id'])){
|
|
|
|
unset($data['id']);
|
|
|
|
}
|
|
|
|
$data = $this->filterRequestData($data,$this->table);
|
|
|
|
$query = $this->formatQuery($condition);
|
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
return $query->update($data);
|
|
...
|
...
|
@@ -251,4 +255,21 @@ class Base extends Model |
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :过滤掉请求数据中不存在于数据库表中的字段
|
|
|
|
* @name :filterRequestData
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2024/6/14 10:49
|
|
|
|
*/
|
|
|
|
public function filterRequestData(array $data, $table)
|
|
|
|
{
|
|
|
|
// 获取表的字段列表
|
|
|
|
$columns = Schema::getColumnListing($table);
|
|
|
|
// 过滤数据
|
|
|
|
return array_filter($data, function ($key) use ($columns) {
|
|
|
|
return in_array($key, $columns);
|
|
|
|
}, ARRAY_FILTER_USE_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|