Home Backend Development PHP Tutorial How the Laravel framework implements CURD operations at the model layer

How the Laravel framework implements CURD operations at the model layer

Jun 13, 2018 pm 05:08 PM
laravel framework model Add, delete, modify and check

This article mainly introduces the Laravel framework to implement the addition, deletion, modification and query (CURD) operation of the model layer. It analyzes the specific implementation techniques of the Laravel framework model model layer to perform the addition, deletion, modification and query operation of the database in the form of examples. Friends in need can refer to the following

The example of this article describes the Laravel framework's implementation of the add, delete, modify, and query (CURD) operation of the model layer. Share it with everyone for your reference, the details are as follows:

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 [];
    }
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Analysis of subviews and form reuse of Laravel5 framework

About Laravel framework database CURD operation and parsing of coherent operations

The above is the detailed content of How the Laravel framework implements CURD operations at the model layer. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

How does Go language implement the addition, deletion, modification and query operations of the database?

How to use model events (Model Events) in Laravel framework How to use model events (Model Events) in Laravel framework Jul 28, 2023 am 10:49 AM

How to use model events (Model Events) in Laravel framework

Trezor Cold Wallet: Model One and Model T Features and Usage Guide Trezor Cold Wallet: Model One and Model T Features and Usage Guide Jan 19, 2024 pm 04:12 PM

Trezor Cold Wallet: Model One and Model T Features and Usage Guide

Java List Interface Example Demonstration: Data Operation to Implement Add, Delete, Modify and Check Operations Java List Interface Example Demonstration: Data Operation to Implement Add, Delete, Modify and Check Operations Dec 20, 2023 am 08:10 AM

Java List Interface Example Demonstration: Data Operation to Implement Add, Delete, Modify and Check Operations

How to use collection framework functions to add, delete, modify and query collections in Java How to use collection framework functions to add, delete, modify and query collections in Java Oct 25, 2023 am 08:45 AM

How to use collection framework functions to add, delete, modify and query collections in Java

Detailed explanation of Model in Django framework Detailed explanation of Model in Django framework Jun 17, 2023 am 08:48 AM

Detailed explanation of Model in Django framework

CRUD operation of MySql: how to quickly complete addition, deletion, modification and query CRUD operation of MySql: how to quickly complete addition, deletion, modification and query Jun 15, 2023 pm 11:30 PM

CRUD operation of MySql: how to quickly complete addition, deletion, modification and query

How to handle the addition, deletion, modification and query operations of form data in Vue technology development How to handle the addition, deletion, modification and query operations of form data in Vue technology development Oct 10, 2023 pm 02:49 PM

How to handle the addition, deletion, modification and query operations of form data in Vue technology development

See all articles