You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. class Slide extends BaseModel
  5. {
  6. //轮播图
  7. protected $table = 'slide';
  8. const TABLE_NAME = 'slide';
  9. public $timestamps = false;
  10. protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  11. const UN_SHOW = 1; // 不显示
  12. const IS_SHOW = 0; // 显示
  13. public static function getDb()
  14. {
  15. return DB::table(self::TABLE_NAME);
  16. }
  17. /**
  18. * 列表
  19. * @param array $where 查询条件
  20. * @param string $order 排序
  21. * @param string $field 字段
  22. * @param int $offset 偏移量
  23. * @param int $limit 取多少条
  24. * @return array
  25. */
  26. public static function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10)
  27. {
  28. $model = self::getDb();
  29. if($where){$model = $model->where($where);}
  30. $res['count'] = $model->count();
  31. $res['list'] = array();
  32. if($res['count'] > 0)
  33. {
  34. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  35. if($order){$model = parent::getOrderByData($model, $order);}
  36. if($offset){}else{$offset = 0;}
  37. if($limit){}else{$limit = 10;}
  38. $res['list'] = $model->skip($offset)->take($limit)->get();
  39. }
  40. return $res;
  41. }
  42. /**
  43. * 分页,用于前端html输出
  44. * @param array $where 查询条件
  45. * @param string $order 排序
  46. * @param string $field 字段
  47. * @param int $limit 每页几条
  48. * @param int $page 当前第几页
  49. * @return array
  50. */
  51. public static function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  52. {
  53. $res = self::getDb();
  54. if($where){$res = $res->where($where);}
  55. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  56. if($order){$res = parent::getOrderByData($res, $order);}
  57. if($limit){}else{$limit = 10;}
  58. return $res->paginate($limit);
  59. }
  60. /**
  61. * 查询全部
  62. * @param array $where 查询条件
  63. * @param string $order 排序
  64. * @param string $field 字段
  65. * @param int $limit 取多少条
  66. * @return array
  67. */
  68. public static function getAll($where = array(), $order = '', $field = '*', $limit = 10, $offset = 0)
  69. {
  70. $res = self::getDb();
  71. if($where){$res = $res->where($where);}
  72. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  73. if($order){$res = parent::getOrderByData($res, $order);}
  74. if($offset){}else{$offset = 0;}
  75. if($limit){}else{$limit = 10;}
  76. $res = $res->skip($offset)->take($limit)->get();
  77. return $res;
  78. }
  79. /**
  80. * 获取一条
  81. * @param array $where 条件
  82. * @param string $field 字段
  83. * @return array
  84. */
  85. public static function getOne($where, $field = '*')
  86. {
  87. $res = self::getDb();
  88. if($where){$res = $res->where($where);}
  89. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  90. $res = $res->first();
  91. return $res;
  92. }
  93. /**
  94. * 添加
  95. * @param array $data 数据
  96. * @return int
  97. */
  98. public static function add(array $data,$type = 0)
  99. {
  100. if($type==0)
  101. {
  102. // 新增单条数据并返回主键值
  103. return self::insertGetId(parent::filterTableColumn($data,self::TABLE_NAME));
  104. }
  105. elseif($type==1)
  106. {
  107. /**
  108. * 添加单条数据
  109. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  110. * 添加多条数据
  111. * $data = [
  112. * ['foo' => 'bar', 'bar' => 'foo'],
  113. * ['foo' => 'bar1', 'bar' => 'foo1'],
  114. * ['foo' => 'bar2', 'bar' => 'foo2']
  115. * ];
  116. */
  117. return self::insert($data);
  118. }
  119. }
  120. /**
  121. * 修改
  122. * @param array $data 数据
  123. * @param array $where 条件
  124. * @return bool
  125. */
  126. public static function edit($data, $where = array())
  127. {
  128. if (self::where($where)->update(parent::filterTableColumn($data, self::TABLE_NAME)) !== false)
  129. {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * 删除
  136. * @param array $where 条件
  137. * @return bool
  138. */
  139. public static function del($where)
  140. {
  141. return self::where($where)->delete();
  142. }
  143. //删除一条记录
  144. public static function remove($id)
  145. {
  146. return self::whereIn('id', explode(',', $id))->delete();
  147. }
  148. //获取显示平台文字:0pc,1weixin,2app,3wap
  149. public static function getTypeText($where)
  150. {
  151. $res = '';
  152. if($where['type'] === 0)
  153. {
  154. $res = 'pc';
  155. }
  156. elseif($where['type'] === 1)
  157. {
  158. $res = 'weixin';
  159. }
  160. elseif($where['type'] === 2)
  161. {
  162. $res = 'app';
  163. }
  164. elseif($where['type'] === 3)
  165. {
  166. $res = 'wap';
  167. }
  168. return $res;
  169. }
  170. }