123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /**
- *------------------------------------------------------
- * Model层基类
- *------------------------------------------------------
- *
- * @author qqiu@qq.com
- * @date 2016/05/26 09:22
- * @version V1.0
- *
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- class BaseModel extends Model
- {
- /**
- * 维护数据表中 created_at 和 updated_at 字段
- */
- public $timestamps = true;
- protected $statusOptions = [
- ['id' => 1, 'name' => '未激活', 'color' => 'gray'],
- ['id' => 2, 'name' => '激活', 'color' => 'blue'],
- ['id' => 3, 'name' => '禁用', 'color' => 'red']
- ];
- /**
- * 多个Where
- * @param Object $query
- * @param array $arr ['status' => 1, 'type' => 2]
- * @return Object $query
- */
- public function multiwhere($query, $arr)
- {
- if ( !is_array($arr) ) {
- return $query;
- }
- foreach ($arr as $key => $value) {
- $query = $query->where($key, $value);
- }
- return $query;
- }
- public function getValidator(Request $request, $type)
- {
- if($type == 'store') {
- $validator = Validator::make($request->input('data'), [
- 'name' => 'required'
- ], [
- 'name.required' => '名称必填'
- ]);
- } else {
- $validator = Validator::make($request->input('data'), [
- 'name' => 'required'
- ], [
- 'name.required' => '名称必填'
- ]);
- }
- return $validator;
- }
- public function getOptions()
- {
- return $this->where('id', '>', 0)->orderBy('sort')->get()->toArray();
- }
- public function getStatusOptions()
- {
- return $this->statusOptions;
- }
- /*
- * type: name|label
- */
- public function getStatus($type = 'name') {
- $options = $this->statusOptions;
- $option = $options[0];
- foreach($options as $item) {
- if($item['id'] == $this['status']) {
- $option = $item;
- break;
- }
- }
- if($type == 'name') return $option['name'];
- else return '<div class="layui-badge layui-bg-' . $option['color'] . '">' . $option['name'] . '</div>';
- }
- public function getNameOrLabel($options = [], $type = 'name', $name = 'status')
- {
- $option = $options[0];
- foreach($options as $item) {
- if($item['id'] == $this[$name]) {
- $option = $item;
- break;
- }
- }
- if($type == 'name') return $option['name'];
- else return '<div class="layui-badge layui-bg-' . $option['color'] . '">' . $option['name'] . '</div>';
- }
- }
|