Home  >  Article  >  PHP Framework  >  How to delete and modify data in Laravel

How to delete and modify data in Laravel

PHPz
PHPzOriginal
2023-03-31 13:52:231223browse

Laravel is a very excellent PHP framework. It is loved by many developers because of its rich functions and convenient operations, including data deletion and modification, which are also commonly used functions. Let's briefly introduce how to delete and modify data in Laravel.

1. Data deletion

1.1 Delete a single piece of data

In Laravel, to delete a single piece of data, you need to first obtain the data object to be deleted through Eloquent, and then call its delete( ) method will do. For example:

$user = User::find(1);
$user->delete();

The above code will delete the User record with ID 1. It should be noted that if the data to be deleted does not exist, an exception will not be thrown, but will be returned directly.

1.2 Batch deletion of data

Sometimes it is necessary to delete a lot of data at the same time. In order to avoid performance problems caused by circular calls to delete(), Laravel provides a combination of where and delete methods to achieve batch deletion. For example:

User::where('age', '<&#39;, 18)->delete();

The above code will delete all User records younger than 18 years old.

1.3 Soft deletion

In actual applications, deleting data is not necessarily physical deletion, it can also be logical deletion. Laravel provides a soft delete feature that marks data as deleted when it is deleted instead of deleting it directly from the database. You need to first define a $dates attribute in the data model:

protected $dates = ['deleted_at'];

Then use soft deletion. Just implement the SoftDeletes interface in the model and call the delete() method:

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
}

$user = User::find(1);
$user->delete();

The above code The deleted_at field of the User record with ID 1 will be set to the current time instead of being deleted directly from the database. You can use the withTrashed() method to query soft-deleted data, or you can use the forceDelete() method to permanently delete soft-deleted data.

2. Data modification

2.1 Modify a single piece of data

In Laravel, modifying a single piece of data is very similar to adding new data. You need to first obtain the data object to be modified through Eloquent, then modify its attribute values, and finally call the save() method. For example:

$user = User::find(1);
$user->name = '小明';
$user->age = 20;
$user->save();

The above code will set the name field of the User record with ID 1 to "Xiao Ming" and the age field to 20.

2.2 Modify data in batches

Laravel provides the update method to modify the specified attribute values ​​​​of multiple pieces of data at the same time. For example:

User::where('age', '<&#39;, 18)->update(['age' => 20]);

The above code will set the age field of all User records younger than 18 years old to 20.

  1. Summary

Deleting and modifying data is one of the most basic operations in Laravel development. The above briefly introduces the functions and usage related to deletion and modification operations in Laravel. . These operations are developed based on Eloquent ORM, which is very convenient and practical.

The above is the detailed content of How to delete and modify data in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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