Laravel Framework Realizes the Addition Deletion and Modification of model Layer Check of CURD Operation Example

  • 2021-09-24 21:50:55
  • OfStack

In this paper, an example of Laravel framework to achieve model layer add, delete, modify and check (CURD) operation. Share it for your reference, as follows:


protected $table = 'user_city';
public $timestamps = false;
// Add   Return id
public function cityadd($data)
{
    return $this->insertGetId($data);
}
// Single search 
public function getfind($id)
{
    if($this->where('id',$id)->first()){
      return $this->where('id',$id)->first()->toArray();
    }else{
      return [];
    }
}
// How many query users are there uid, Quantity returned 
public function countCity($uid){
    if($this->where('uid',$uid)->first()){
      return $this->where('uid',$uid)->count();
    }else{
      return [];
    }
}
// Query all data 
public function getAll()
{
    return $this->get()->toArray();
}
/**
*  Modify administrator information 
* @param $id
* @param $data
* @return bool
*/
public function upAdmin($id,$data)
{
    if($this->find($id)){
      return $this->where('id',$id)->update($data);
    }else{
      return false;
    }
}
// Add conditions and time 
// Query the number of cities subscribed by users 
public function buy_num($uid){
    $startDate = date('Y-m-01', strtotime(date("Y-m-d")));
    $endDate = date('Y-m-d', strtotime("$startDate +1 month -1 day"));
    //  Convert a date to Unix Time stamp 
    $endDate=$endDate." 22:59:59";
    $startDateStr = strtotime($startDate);
    $endtDateStr = strtotime($endDate);
    return $this->where('uid',$uid)->where('buy_type',1)->whereBetween('create_time', array($startDateStr,$endtDateStr))->sum('buy_num');
}
/**
*  According to id Find city information   Returns only the value of a field 
* @param $id
* @return array
*/
public function getCityName($id)
{
    if($this->where('city_id',$id)->first()){
      return $this->where('city_id',$id)->lists('city_name')[0];
    }else{
      return [];
    }
}

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on the framework of Laravel PHP programming help.


Related articles: