1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?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 $guarded = [];
- 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)
- {
- $validator = Validator::make($request->input('data'), [
- 'user_id' => 'required'
- ], [
- 'user_id.required' => '用户必填'
- ]);
- return $validator;
- }
- public static function getOptions()
- {
- return self::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>';
- }
- }
|