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.

134 lines
3.7 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
  1. <?php
  2. namespace App\Http\Model;
  3. class Article extends BaseModel
  4. {
  5. //文章模型
  6. /**
  7. * 关联到模型的数据表
  8. *
  9. * @var string
  10. */
  11. protected $table = 'article';
  12. /**
  13. * 表明模型是否应该被打上时间戳
  14. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  15. *
  16. * @var bool
  17. */
  18. public $timestamps = false;
  19. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  20. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  21. /**
  22. * The connection name for the model.
  23. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  24. * @var string
  25. */
  26. //protected $connection = 'connection-name';
  27. const IS_CHECK = 0; // 已审核
  28. const UN_CHECK = 1; // 未审核
  29. //常用字段
  30. protected static $common_field = array(
  31. 'id', 'typeid', 'tuijian', 'click', 'title', 'writer', 'source','litpic', 'pubdate', 'addtime', 'description', 'listorder'
  32. );
  33. /**
  34. * 获取关联到文章的分类
  35. */
  36. public function arctype()
  37. {
  38. return $this->belongsTo(Arctype::class, 'typeid', 'id');
  39. }
  40. public static function getList(array $param)
  41. {
  42. extract($param); //参数:group_id,limit,offset
  43. $limit = isset($limit) ? $limit : 10;
  44. $offset = isset($offset) ? $offset : 0;
  45. $model = new Article;
  46. if(isset($typeid)){$where['typeid'] = $typeid;}
  47. if(isset($ischeck)){$where['ischeck'] = $ischeck;}
  48. if(isset($keyword)){$model = $model->where("title", "like", "%$keyword%");} //关键词搜索
  49. if($where){$model = $model->where($where);}
  50. $res['count'] = $model->count();
  51. $res['list'] = array();
  52. //排序
  53. if(isset($orderby))
  54. {
  55. switch ($orderby)
  56. {
  57. case 1:
  58. $model = $model->orderBy('click','desc'); //点击量从高到低
  59. break;
  60. case 2:
  61. $model = $model->orderBy('listorder','desc'); //排序
  62. break;
  63. case 3:
  64. $model = $model->orderBy('pubdate','desc'); //更新时间从高到低
  65. break;
  66. default:
  67. $model = $model->orderBy('addtime','desc'); //添加时间从高到低
  68. }
  69. }
  70. if($res['count']>0)
  71. {
  72. $res['list'] = $model->select(self::$common_field)->orderBy('id', 'desc')->skip($offset)->take($limit)->get();
  73. }
  74. return $res;
  75. }
  76. public static function getOne(array $param)
  77. {
  78. extract($param);
  79. $where['id'] = $id;
  80. if(isset($ischeck)){$where['ischeck'] = $ischeck;}
  81. return self::where($where)->first();
  82. }
  83. public static function add(array $data)
  84. {
  85. if ($id = self::insertGetId($data))
  86. {
  87. return $id;
  88. }
  89. return false;
  90. }
  91. public static function modify($where, array $data)
  92. {
  93. if (self::where($where)->update($data)!==false)
  94. {
  95. return true;
  96. }
  97. return false;
  98. }
  99. //删除一条记录
  100. public static function remove($id)
  101. {
  102. if (!self::whereIn('id', explode(',', $id))->delete())
  103. {
  104. return false;
  105. }
  106. return true;
  107. }
  108. }