42 lines
996 B
PHP
42 lines
996 B
PHP
<?php
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
*/
|
|
|
|
namespace App\Utils;
|
|
|
|
use function Hyperf\Support\make;
|
|
|
|
trait Crud
|
|
{
|
|
public static function add(array $data)
|
|
{
|
|
$data['create_time'] = date("Y-m-d H:i:s");
|
|
return self::insertGetId($data);
|
|
}
|
|
|
|
public static function edit(array $data)
|
|
{
|
|
$model = make(static::class);
|
|
return $model->where($model->primaryKey, $data[$model->primaryKey])->update($data);
|
|
}
|
|
|
|
public static function del(array $ids = null): int
|
|
{
|
|
if (empty($ids)) {
|
|
$param = Param::only(['ids' => []]);
|
|
$ids = $param['ids'];
|
|
}
|
|
if (count($ids) <= 0) {
|
|
return false;
|
|
}
|
|
return self::destroy($ids);
|
|
}
|
|
|
|
public static function getById(int $id, array $field = ['*']): array
|
|
{
|
|
$model = make(static::class);
|
|
$info = $model->where($model->primaryKey, $id)->select($field)->first();
|
|
return $info ? $info->toArray() : [];
|
|
}
|
|
} |