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.

163 lines
4.6 KiB

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