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.

212 lines
6.1 KiB

8 years ago
4 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 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
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. use Illuminate\Support\Facades\Log;
  5. class Article extends BaseModel
  6. {
  7. //文章模型
  8. /**
  9. * 关联到模型的数据表
  10. *
  11. * @var string
  12. */
  13. protected $table = 'article';
  14. /**
  15. * 表明模型是否应该被打上时间戳
  16. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  17. *
  18. * @var bool
  19. */
  20. public $timestamps = false;
  21. protected $hidden = array();
  22. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  23. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  24. /**
  25. * The connection name for the model.
  26. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  27. * @var string
  28. */
  29. //protected $connection = 'connection-name';
  30. const IS_CHECK = 0; // 已审核
  31. const UN_CHECK = 1; // 未审核
  32. //常用字段
  33. protected $common_field = array(
  34. 'id', 'typeid', 'tuijian', 'click', 'title', 'writer', 'source','litpic', 'pubdate', 'addtime', 'description', 'listorder'
  35. );
  36. public function getDb()
  37. {
  38. return DB::table($this->table);
  39. }
  40. /**
  41. * 获取关联到文章的分类
  42. */
  43. public function arctype()
  44. {
  45. return $this->belongsTo(Arctype::class, 'typeid', 'id');
  46. }
  47. /**
  48. * 列表
  49. * @param array $where 查询条件
  50. * @param string $order 排序
  51. * @param string $field 字段
  52. * @param int $offset 偏移量
  53. * @param int $limit 取多少条
  54. * @return array
  55. */
  56. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  57. {
  58. $model = $this->getDb();
  59. if($where){$model = $model->where($where);}
  60. $res['count'] = $model->count();
  61. $res['list'] = array();
  62. if($res['count'] > 0)
  63. {
  64. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  65. if($order){$model = parent::getOrderByData($model, $order);}
  66. if($offset){}else{$offset = 0;}
  67. if($limit){}else{$limit = 15;}
  68. $res['list'] = $model->skip($offset)->take($limit)->get();
  69. }
  70. return $res;
  71. }
  72. /**
  73. * 分页,用于前端html输出
  74. * @param array $where 查询条件
  75. * @param string $order 排序
  76. * @param string $field 字段
  77. * @param int $limit 每页几条
  78. * @param int $page 当前第几页
  79. * @return array
  80. */
  81. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  82. {
  83. $res = $this->getDb();
  84. if($where){$res = $res->where($where);}
  85. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  86. if($order){$res = parent::getOrderByData($res, $order);}
  87. if($limit){}else{$limit = 15;}
  88. return $res->paginate($limit);
  89. }
  90. /**
  91. * 查询全部
  92. * @param array $where 查询条件
  93. * @param string $order 排序
  94. * @param string $field 字段
  95. * @param int $limit 取多少条
  96. * @return array
  97. */
  98. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  99. {
  100. $res = $this->getDb();
  101. if($where){$res = $res->where($where);}
  102. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  103. if($order){$res = parent::getOrderByData($res, $order);}
  104. if($offset){$res = $res->skip($offset);}
  105. if($limit){$res = $res->take($limit);}
  106. $res = $res->get();
  107. return $res;
  108. }
  109. /**
  110. * 获取一条
  111. * @param array $where 条件
  112. * @param string $field 字段
  113. * @return array
  114. */
  115. public function getOne($where, $field = '*')
  116. {
  117. $res = $this->getDb();
  118. if($where){$res = $res->where($where);}
  119. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  120. $res = $res->first();
  121. return $res;
  122. }
  123. /**
  124. * 添加
  125. * @param array $data 数据
  126. * @return int
  127. */
  128. public function add(array $data,$type = 0)
  129. {
  130. if($type==0)
  131. {
  132. // 新增单条数据并返回主键值
  133. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  134. }
  135. elseif($type==1)
  136. {
  137. /**
  138. * 添加单条数据
  139. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  140. * 添加多条数据
  141. * $data = [
  142. * ['foo' => 'bar', 'bar' => 'foo'],
  143. * ['foo' => 'bar1', 'bar' => 'foo1'],
  144. * ['foo' => 'bar2', 'bar' => 'foo2']
  145. * ];
  146. */
  147. return self::insert($data);
  148. }
  149. }
  150. /**
  151. * 修改
  152. * @param array $data 数据
  153. * @param array $where 条件
  154. * @return int
  155. */
  156. public function edit($data, $where = array())
  157. {
  158. $res = $this->getDb();
  159. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  160. }
  161. /**
  162. * 删除
  163. * @param array $where 条件
  164. * @return bool
  165. */
  166. public function del($where)
  167. {
  168. $res = $this->getDb();
  169. $res = $res->where($where)->delete();
  170. return $res;
  171. }
  172. //是否审核
  173. public function getIscheckAttr($data)
  174. {
  175. $arr = array(0 => '已审核', 1 => '未审核');
  176. return $arr[$data->ischeck];
  177. }
  178. //获取栏目名称
  179. public function getTypenameAttr($data)
  180. {
  181. return DB::table('arctype')->where(array('id'=>$data['typeid']))->value('name');
  182. }
  183. }