当前位置:Gxlcms > PHP教程 > Laravel框架实现model层的增删改查(CURD)操作示例

Laravel框架实现model层的增删改查(CURD)操作示例

时间:2021-07-01 10:21:17 帮助过:13人阅读

这篇文章主要介绍了Laravel框架实现model层的增删改查(CURD)操作,结合实例形式分析了Laravel框架模型model层进行数据库的增删改查操作具体实现技巧,需要的朋友可以参考下

本文实例讲述了Laravel框架实现model层的增删改查(CURD)操作。分享给大家供大家参考,具体如下:

  1. protected $table = 'user_city';
  2. public $timestamps = false;
  3. //添加 返回id
  4. public function cityadd($data)
  5. {
  6. return $this->insertGetId($data);
  7. }
  8. //单条查找
  9. public function getfind($id)
  10. {
  11. if($this->where('id',$id)->first()){
  12. return $this->where('id',$id)->first()->toArray();
  13. }else{
  14. return [];
  15. }
  16. }
  17. //查询用户有几个uid,返回数量
  18. public function countCity($uid){
  19. if($this->where('uid',$uid)->first()){
  20. return $this->where('uid',$uid)->count();
  21. }else{
  22. return [];
  23. }
  24. }
  25. //查询全部数据
  26. public function getAll()
  27. {
  28. return $this->get()->toArray();
  29. }
  30. /**
  31. * 修改管理员信息
  32. * @param $id
  33. * @param $data
  34. * @return bool
  35. */
  36. public function upAdmin($id,$data)
  37. {
  38. if($this->find($id)){
  39. return $this->where('id',$id)->update($data);
  40. }else{
  41. return false;
  42. }
  43. }
  44. //加条件,时间
  45. //查询用户的认购的城数
  46. public function buy_num($uid){
  47. $startDate = date('Y-m-01', strtotime(date("Y-m-d")));
  48. $endDate = date('Y-m-d', strtotime("$startDate +1 month -1 day"));
  49. // 将日期转换为Unix时间戳
  50. $endDate=$endDate." 22:59:59";
  51. $startDateStr = strtotime($startDate);
  52. $endtDateStr = strtotime($endDate);
  53. return $this->where('uid',$uid)->where('buy_type',1)->whereBetween('create_time', array($startDateStr,$endtDateStr))->sum('buy_num');
  54. }
  55. /**
  56. * 根据id查找城池信息 只返回某个字段的值
  57. * @param $id
  58. * @return array
  59. */
  60. public function getCityName($id)
  61. {
  62. if($this->where('city_id',$id)->first()){
  63. return $this->where('city_id',$id)->lists('city_name')[0];
  64. }else{
  65. return [];
  66. }
  67. }

以上就是本篇文章的全部内容了,感谢大家阅读。更多请关注PHP中文网!

相关推荐:

laravel框架实现搜索功能代码解析

Laravel框架实现redis集群详解

以上就是Laravel框架实现model层的增删改查(CURD)操作示例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行