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

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

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

本文实例讲述了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. }

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

人气教程排行