Home > PHP Framework > Laravel > body text

laravel db delete

PHPz
Release: 2023-05-21 09:04:37
Original
449 people have browsed it

Laravel DB Delete

Laravel is a very popular PHP framework. Due to its simplicity, ease of use and powerful functions, more and more developers are starting to use it. In Laravel applications, it is very common to use databases, but during use, we may encounter data deletion. This article will introduce how to delete data in the database in Laravel.

Delete operations in Laravel can use Eloquent ORM or query builder. Let’s first introduce how to delete using Eloquent ORM.

  1. Delete using Eloquent ORM

In Laravel, Eloquent ORM is a very powerful and easy-to-use tool. It helps us interact with the database more easily. We can delete one or more database records using Eloquent ORM.

1.1 Single record deletion

The code to delete a single record is very simple, just use the delete method. For example, we have a User model, which corresponds to a users table:

// 删除id为1的用户
$user = AppUser::find(1);
$user->delete();
Copy after login

The above code will delete the record with id 1 in the users table.

1.2 Multiple record deletion

If we want to delete multiple records, we can use the where condition to filter. For example, if we want to delete all users older than 30 years old, we can use the following code:

// 删除年龄大于30岁的用户
$users = AppUser::where('age', '>', 30)->get();
$users->delete();
Copy after login

The above code will delete all records older than 30 years old in the users table.

  1. Use query builder for deletion

In addition to using Eloquent ORM for deletion, we can also use query builder for deletion. The query builder provides a more flexible way to build SQL query statements.

2.1 Single record deletion

Like Eloquent ORM, we can use the query builder to delete a single record. For example, we have a users table which contains a field called id. To delete the record with id 1, you can use the following code:

// 删除id为1的用户
DB::table('users')->where('id', '=', 1)->delete();
Copy after login

The above code will delete the record with id 1 in the users table.

2.2 Multiple record deletion

If we want to delete multiple records, we can use whereIn conditions to filter. For example, if we want to delete users with IDs 1, 2, and 3, we can use the following code:

// 删除id为1、2、3的用户
DB::table('users')->whereIn('id', [1, 2, 3])->delete();
Copy after login

The above code will delete records with IDs 1, 2, and 3 in the users table.

Summary

In Laravel, deleting database records is very easy. We can use Eloquent ORM or query builder for deletion. When deleting records, make sure you have confirmed that you are deleting the correct objects and follow best practices to ensure application stability.

The above is the detailed content of laravel db delete. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!