当前位置:首页 > CMS教程 > 其它CMS > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-17 10:28:47 浏览: 评论:0 

这篇文章主要介绍了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. }

Tags: Laravel框架 model增删改查

分享到: