search
HomePHP FrameworkThinkPHPHow to use the delete method in thinkphp

How to use the delete method in thinkphp

May 27, 2023 pm 01:24 PM
thinkphp

1. Use the delete method

In ThinkPHP, we can use the delete method to delete data. This method is very simple and only needs to be used in the model. Just use the delete method, for example:

$user = UserModel::get(1);
$user->delete();

This way you can delete the user with ID 1. If your Model does not specify a primary key, you can use the following method:

$user = UserModel::get(['name' => 'thinkphp']);
$user->delete();

In this way, you can delete the user named thinkphp.

2. Chain deletion

In ThinkPHP, we can also use chain deletion to delete data. This method is more commonly used, such as:

$user = UserModel::where('id', 1)->delete();

This method will delete the user data with ID 1 and return the number of deleted rows.

3. Soft deletion

In ThinkPHP, we can also use soft deletion to delete data. The so-called soft deletion means marking the data as deleted rather than actually deleting the data. This operation is useful in data recovery, data query, etc. We can use soft deletion in the model, for example:

class UserModel extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $defaultSoftDelete = 0;

    protected $autoWriteTimestamp = true;
}

In this example, we use the Trait of SoftDelete and set the delete_time field to the deletion time. In this way, when we use the delete method or chain deletion method, the corresponding data will be marked as deleted instead of actually deleting the data.

4. Batch deletion

When developing projects, we sometimes need to delete data in batches. There are two methods:

1. Use SQL Statement

We can directly use SQL statements to delete data in batches, for example:

Db::table('user')->where('id', 'in', [1, 2, 3])->delete();

This method will delete user data with IDs 1, 2, and 3, and return the number of deleted rows.

2. Use the delete method of the model

We can also use the delete method of the model to delete data in batches, for example:

UserModel::destroy([1, 2, 3]);

This method will delete the IDs 1 and 2 , 3 user data, and returns the number of deleted rows.

The above is the detailed content of How to use the delete method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools