>  기사  >  백엔드 개발  >  Laravel 프레임워크는 모델 레이어에서 작업을 추가, 삭제, 수정 및 확인하는 예제를 구현합니다.

Laravel 프레임워크는 모델 레이어에서 작업을 추가, 삭제, 수정 및 확인하는 예제를 구현합니다.

jacklove
jacklove원래의
2018-06-23 17:01:273320검색

이 글에서는 모델 계층의 추가, 삭제, 수정 및 확인(CURD) 작업을 구현하는 Laravel 프레임워크를 주로 소개합니다. 추가, 삭제, 수정 및 확인을 수행하는 Laravel 프레임워크 모델 모델 계층의 구체적인 구현 기술을 분석합니다. 도움이 필요한 친구들이 참고할 수 있습니다

이 글의 예시는 Laravel 프레임워크가 모델 레이어의 추가, 삭제, 수정 및 쿼리(CURD) 작업을 구현하는 방법을 설명합니다. 다음과 같이 참조용으로 모든 사람과 공유하세요.

protected $table = 'user_city';
public $timestamps = false;
//添加 返回id
public function cityadd($data)
{
    return $this->insertGetId($data);
}
//单条查找
public function getfind($id)
{
    if($this->where('id',$id)->first()){
      return $this->where('id',$id)->first()->toArray();
    }else{
      return [];
    }
}
//查询用户有几个uid,返回数量
public function countCity($uid){
    if($this->where('uid',$uid)->first()){
      return $this->where('uid',$uid)->count();
    }else{
      return [];
    }
}
//查询全部数据
public function getAll()
{
    return $this->get()->toArray();
}
/**
* 修改管理员信息
* @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;
    }
}
//加条件,时间
//查询用户的认购的城数
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"));
    // 将日期转换为Unix时间戳
    $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');
}
/**
* 根据id查找城池信息 只返回某个字段的值
* @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 [];
    }
}

관심을 가질 만한 기사:

Excel 데이터를 내보내는 방법에 대한 ThinkPHP 프레임워크 예

Native JS는 POST를 통해 Ajax를 구현합니다. PHP PHP 기술과 상호 작용하는 방법 예제

Laravel Geetest 인증 코드 통합 방법 PHP 예제

위 내용은 Laravel 프레임워크는 모델 레이어에서 작업을 추가, 삭제, 수정 및 확인하는 예제를 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.