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.

227 lines
5.9 KiB

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