catchAdmin/extend/catcher/traits/db/BaseOptionsTrait.php

79 lines
1.4 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
namespace catcher\traits\db;
trait BaseOptionsTrait
{
/**
*
* @time 2019年12月03日
* @param array $data
* @return bool
*/
public function storeBy(array $data)
{
2019-12-12 09:14:08 +08:00
if ($this->allowField($this->field)->save($data)) {
2019-12-07 17:31:38 +08:00
return $this->{$this->getPk()};
2019-12-06 09:17:40 +08:00
}
return false;
}
/**
*
* @time 2019年12月03日
* @param $id
* @param $data
* @return bool
*/
public function updateBy($id, $data)
{
2019-12-12 09:14:08 +08:00
if (static::update($data, [$this->getPk() => $id], $this->field)) {
return true;
2019-12-06 09:17:40 +08:00
}
return false;
}
/**
*
* @time 2019年12月03日
* @param $id
* @param array $field
2019-12-07 17:31:38 +08:00
* @param bool $trash
2019-12-06 09:17:40 +08:00
* @return mixed
*/
2019-12-07 17:31:38 +08:00
public function findBy($id, array $field = ['*'], $trash = false)
2019-12-06 09:17:40 +08:00
{
2019-12-07 17:31:38 +08:00
if ($trash) {
return static::onlyTrashed()->find($id);
}
2019-12-26 09:03:19 +08:00
2019-12-07 17:31:38 +08:00
return static::where($this->getPk(), $id)->field($field)->find();
2019-12-06 09:17:40 +08:00
}
/**
*
* @time 2019年12月03日
* @param $id
2019-12-07 17:31:38 +08:00
* @param $force
* @return mixed
*/
public function deleteBy($id, $force = false)
{
return static::destroy($id, $force);
}
/**
* @time 2019年12月07日
* @param $id
2019-12-06 09:17:40 +08:00
* @return mixed
*/
2019-12-07 17:31:38 +08:00
public function recover($id)
2019-12-06 09:17:40 +08:00
{
2019-12-07 17:31:38 +08:00
return static::onlyTrashed()->find($id)->restore();
2019-12-06 09:17:40 +08:00
}
}