主题
Eloquent:入门
简介
Laravel 包含 Eloquent,一个对象关系映射器(ORM),使与数据库的交互变得愉快。使用 Eloquent 时,每个数据库表都有一个对应的"模型"用于与该表交互。除了从数据库表中检索记录外,Eloquent 模型还允许你从表中插入、更新和删除记录。
NOTE
在开始之前,请确保在应用程序的 config/database.php 配置文件中配置了数据库连接。有关配置数据库的更多信息,请查看数据库配置文档。
生成模型类
首先,让我们创建一个 Eloquent 模型。模型通常位于 app\Models 目录中,并继承 Illuminate\Database\Eloquent\Model 类。你可以使用 make:model Artisan 命令来生成新模型:
shell
php artisan make:model Flight如果你想在生成模型时同时生成数据库迁移,可以使用 --migration 或 -m 选项:
shell
php artisan make:model Flight --migration在生成模型时,你可以生成各种其他类型的类,例如工厂、填充器、策略、控制器和表单请求。此外,这些选项可以组合使用以一次创建多个类:
shell
# 生成模型和 FlightFactory 类...
php artisan make:model Flight --factory
php artisan make:model Flight -f
# 生成模型和 FlightSeeder 类...
php artisan make:model Flight --seed
php artisan make:model Flight -s
# 生成模型和 FlightController 类...
php artisan make:model Flight --controller
php artisan make:model Flight -c
# 生成模型、FlightController 资源类和表单请求类...
php artisan make:model Flight --controller --resource --requests
php artisan make:model Flight -crR
# 生成模型和 FlightPolicy 类...
php artisan make:model Flight --policy
# 生成模型、迁移、工厂、填充器和控制器...
php artisan make:model Flight -mfsc
# 快捷生成模型、迁移、工厂、填充器、策略、控制器和表单请求...
php artisan make:model Flight --all
php artisan make:model Flight -a
# 生成中间表模型...
php artisan make:model Member --pivot
php artisan make:model Member -p检查模型
有时仅通过浏览代码很难确定模型的所有可用属性和关联关系。你可以尝试使用 model:show Artisan 命令,它提供了模型所有属性和关联关系的便捷概览:
shell
php artisan model:show FlightEloquent 模型约定
由 make:model 命令生成的模型将放置在 app/Models 目录中。让我们检查一个基本的模型类,并讨论 Eloquent 的一些关键约定:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
// ...
}表名
通过上面的示例,你可能已经注意到我们没有告诉 Eloquent 哪个数据库表对应我们的 Flight 模型。按照约定,类名的"蛇形命名法"复数形式将被用作表名,除非明确指定了另一个名称。因此,在这种情况下,Eloquent 将假设 Flight 模型将记录存储在 flights 表中,而 AirTrafficController 模型将记录存储在 air_traffic_controllers 表中。
如果你的模型对应的数据库表不符合此约定,你可以使用 Table 属性手动指定模型的表名:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table('my_flights')]
class Flight extends Model
{
// ...
}主键
Eloquent 还会假设每个模型对应的数据库表有一个名为 id 的主键列。如有必要,你可以使用 Table 属性的 key 参数指定一个不同的列作为模型的主键:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table(key: 'flight_id')]
class Flight extends Model
{
// ...
}此外,Eloquent 假设主键是一个自增整数值,这意味着 Eloquent 会自动将主键转换为整数。如果你希望使用非自增或非数字主键,应在 Table 属性上指定 keyType 和 incrementing 参数:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table(key: 'uuid', keyType: 'string', incrementing: false)]
class Flight extends Model
{
// ...
}"复合"主键
Eloquent 要求每个模型至少有一个可以作为主键的唯一标识"ID"。Eloquent 模型不支持"复合"主键。但是,除了表的唯一标识主键之外,你可以自由地向数据库表添加额外的多列唯一索引。
UUID 和 ULID 键
你可以选择使用 UUID 代替自增整数作为 Eloquent 模型的主键。UUID 是长度为 36 个字符的通用唯一字母数字标识符。
如果你希望模型使用 UUID 键而不是自增整数键,可以在模型上使用 Illuminate\Database\Eloquent\Concerns\HasUuids trait。当然,你应该确保模型具有 UUID 等效的主键列:
php
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasUuids;
// ...
}
$article = Article::create(['title' => 'Traveling to Europe']);
$article->id; // "018f2b5c-6a7f-7b12-9d6f-2f8a4e0c9c11"默认情况下,HasUuids trait 会为你的模型生成 UUIDv7 标识符。这些 UUID 对于索引数据库存储更高效,因为它们可以按字典序排序。
你可以通过在模型上定义 newUniqueId 方法来覆盖给定模型的 UUID 生成过程。此外,你可以通过在模型上定义 uniqueIds 方法来指定哪些列应接收 UUID:
php
use Ramsey\Uuid\Uuid;
/**
* Generate a new UUID for the model.
*/
public function newUniqueId(): string
{
return (string) Uuid::uuid4();
}
/**
* Get the columns that should receive a unique identifier.
*
* @return array<int, string>
*/
public function uniqueIds(): array
{
return ['id', 'discount_code'];
}如果你愿意,可以选择使用"ULID"代替 UUID。ULID 与 UUID 类似;但长度仅为 26 个字符。与有序 UUID 一样,ULID 可按字典序排序以实现高效的数据库索引。要使用 ULID,你应该在模型上使用 Illuminate\Database\Eloquent\Concerns\HasUlids trait。你还应该确保模型具有 ULID 等效的主键列:
php
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasUlids;
// ...
}
$article = Article::create(['title' => 'Traveling to Asia']);
$article->id; // "01gd4d3tgrrfqeda94gdbtdk5c"时间戳
默认情况下,Eloquent 期望模型对应的数据库表上存在 created_at 和 updated_at 列。Eloquent 会在创建或更新模型时自动设置这些列的值。如果你不希望这些列由 Eloquent 自动管理,可以在模型的 Table 属性上将 timestamps 设置为 false:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table(timestamps: false)]
class Flight extends Model
{
// ...
}如果你需要自定义模型时间戳的格式,可以使用 Table 属性的 dateFormat 参数。此参数决定日期属性在数据库中的存储方式以及模型序列化为数组或 JSON 时的格式:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
#[Table(dateFormat: 'U')]
class Flight extends Model
{
// ...
}如果你需要自定义用于存储时间戳的列名,可以在模型上定义 CREATED_AT 和 UPDATED_AT 常量:
php
<?php
class Flight extends Model
{
/**
* The name of the "created at" column.
*
* @var string|null
*/
public const CREATED_AT = 'creation_date';
/**
* The name of the "updated at" column.
*
* @var string|null
*/
public const UPDATED_AT = 'updated_date';
}如果你想在不修改模型 updated_at 时间戳的情况下执行模型操作,可以在传递给 withoutTimestamps 方法的闭包中操作模型:
php
Model::withoutTimestamps(fn () => $post->increment('reads'));数据库连接
默认情况下,所有 Eloquent 模型将使用为应用程序配置的默认数据库连接。如果你想指定与特定模型交互时应使用的不同连接,可以使用 Connection 属性:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Connection;
use Illuminate\Database\Eloquent\Model;
#[Connection('mysql')]
class Flight extends Model
{
// ...
}默认属性值
默认情况下,新实例化的模型实例不会包含任何属性值。如果你想为模型的某些属性定义默认值,可以在模型上定义 $attributes 属性。放置在 $attributes 数组中的属性值应该是其原始的"可存储"格式,就像刚从数据库中读取的一样:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'options' => '[]',
'delayed' => false,
];
}配置 Eloquent 严格模式
Laravel 提供了多种方法,允许你在各种情况下配置 Eloquent 的行为和"严格模式"。
首先,preventLazyLoading 方法接受一个可选的布尔参数,指示是否应阻止延迟加载。例如,你可能希望仅在非生产环境中禁用延迟加载,以便生产环境即使在生产代码中意外存在延迟加载的关联关系时仍能正常运行。通常,应在应用程序的 AppServiceProvider 的 boot 方法中调用此方法:
php
use Illuminate\Database\Eloquent\Model;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Model::preventLazyLoading(! $this->app->isProduction());
}此外,你可以通过调用 preventSilentlyDiscardingAttributes 方法来指示 Laravel 在尝试填充不可填充属性时抛出异常。这有助于在本地开发期间防止意外错误,即当尝试设置未添加到模型的 fillable 数组中的属性时:
php
Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());检索模型
一旦你创建了模型及其关联的数据库表,就可以开始从数据库中检索数据了。你可以将每个 Eloquent 模型视为一个强大的查询构建器,允许你流畅地查询与模型关联的数据库表。模型的 all 方法将检索模型关联数据库表中的所有记录:
php
use App\Models\Flight;
foreach (Flight::all() as $flight) {
echo $flight->name;
}构建查询
Eloquent 的 all 方法将返回模型表中的所有结果。但是,由于每个 Eloquent 模型都充当查询构建器,你可以向查询添加额外的约束,然后调用 get 方法来检索结果:
php
$flights = Flight::where('active', 1)
->orderBy('name')
->limit(10)
->get();NOTE
由于 Eloquent 模型是查询构建器,你应该查看 Laravel 查询构建器提供的所有方法。你可以在编写 Eloquent 查询时使用这些方法中的任何一个。
刷新模型
如果你已经拥有从数据库中检索到的 Eloquent 模型实例,可以使用 fresh 和 refresh 方法"刷新"模型。fresh 方法将从数据库中重新检索模型。现有模型实例不会受到影响:
php
$flight = Flight::where('number', 'FR 900')->first();
$freshFlight = $flight->fresh();refresh 方法将使用数据库中的新数据重新填充现有模型。此外,所有已加载的关联关系也将被刷新:
php
$flight = Flight::where('number', 'FR 900')->first();
$flight->number = 'FR 456';
$flight->refresh();
$flight->number; // "FR 900"集合
正如我们所见,像 all 和 get 这样的 Eloquent 方法从数据库中检索多条记录。但是,这些方法不返回普通的 PHP 数组。而是返回 Illuminate\Database\Eloquent\Collection 的实例。
Eloquent Collection 类继承了 Laravel 的基础 Illuminate\Support\Collection 类,该类提供了各种有用的方法用于与数据集合交互。例如,reject 方法可以根据调用闭包的结果从集合中移除模型:
php
$flights = Flight::where('destination', 'Paris')->get();
$flights = $flights->reject(function (Flight $flight) {
return $flight->cancelled;
});除了 Laravel 基础集合类提供的方法之外,Eloquent 集合类还提供了一些额外的方法,专门用于与 Eloquent 模型集合交互。
由于 Laravel 的所有集合都实现了 PHP 的可迭代接口,你可以像遍历数组一样遍历集合:
php
foreach ($flights as $flight) {
echo $flight->name;
}分块结果
如果你尝试通过 all 或 get 方法加载数以万计的 Eloquent 记录,应用程序可能会耗尽内存。与其使用这些方法,chunk 方法可以更高效地处理大量模型。
chunk 方法将检索 Eloquent 模型的子集,并将它们传递给闭包进行处理。由于每次只检索当前块的 Eloquent 模型,chunk 方法在处理大量模型时将显著减少内存使用:
php
use App\Models\Flight;
use Illuminate\Database\Eloquent\Collection;
Flight::chunk(200, function (Collection $flights) {
foreach ($flights as $flight) {
// ...
}
});传递给 chunk 方法的第一个参数是你希望每个"块"接收的记录数。作为第二个参数传递的闭包将对从数据库中检索到的每个块被调用。将执行数据库查询来检索传递给闭包的每个记录块。
如果你在 chunk 方法的结果上基于一个同时在迭代过程中更新的列进行过滤,应该使用 chunkById 方法。在这些场景中使用 chunk 方法可能会导致意外和不一致的结果。在内部,chunkById 方法始终会检索 id 列大于前一块中最后一个模型的模型:
php
Flight::where('departed', true)
->chunkById(200, function (Collection $flights) {
$flights->each->update(['departed' => false]);
}, column: 'id');由于 chunkById 和 lazyById 方法会向正在执行的查询添加自己的"where"条件,你通常应该在闭包中逻辑分组你自己的条件:
php
Flight::where(function ($query) {
$query->where('delayed', true)->orWhere('cancelled', true);
})->chunkById(200, function (Collection $flights) {
$flights->each->update([
'departed' => false,
'cancelled' => true
]);
}, column: 'id');使用惰性集合分块
lazy 方法的工作方式类似于 chunk 方法,在底层它以块为单位执行查询。但是,lazy 方法不是将每个块直接传递给回调,而是返回一个扁平化的 Eloquent 模型 LazyCollection,让你可以将结果作为单个流进行交互:
php
use App\Models\Flight;
foreach (Flight::lazy() as $flight) {
// ...
}如果你在 lazy 方法的结果上基于一个同时在迭代过程中更新的列进行过滤,应该使用 lazyById 方法。在内部,lazyById 方法始终会检索 id 列大于前一块中最后一个模型的模型:
php
Flight::where('departed', true)
->lazyById(200, column: 'id')
->each->update(['departed' => false]);你可以使用 lazyByIdDesc 方法基于 id 的降序过滤结果。
游标
与 lazy 方法类似,cursor 方法可用于在遍历数以万计的 Eloquent 模型记录时显著减少应用程序的内存消耗。
cursor 方法只会执行一次数据库查询;然而,各个 Eloquent 模型在实际被迭代之前不会被水合。因此,在遍历游标时,任何给定时间内存中只保留一个 Eloquent 模型。
WARNING
由于 cursor 方法每次只在内存中保留一个 Eloquent 模型,因此它无法预加载关联关系。如果你需要预加载关联关系,请考虑使用 lazy 方法。
在内部,cursor 方法使用 PHP 生成器来实现此功能:
php
use App\Models\Flight;
foreach (Flight::where('destination', 'Zurich')->cursor() as $flight) {
// ...
}cursor 返回一个 Illuminate\Support\LazyCollection 实例。惰性集合允许你使用典型 Laravel 集合上可用的许多集合方法,同时每次只将单个模型加载到内存中:
php
use App\Models\User;
$users = User::cursor()->filter(function (User $user) {
return $user->id > 500;
});
foreach ($users as $user) {
echo $user->id;
}虽然 cursor 方法使用的内存远少于常规查询(因为每次只在内存中保留一个 Eloquent 模型),但它最终仍会耗尽内存。这是因为 PHP 的 PDO 驱动程序在其缓冲区中内部缓存所有原始查询结果。如果你处理的 Eloquent 记录数量非常大,请考虑使用 lazy 方法。
高级子查询
子查询选择
Eloquent 还提供高级子查询支持,允许你在单个查询中从相关表中提取信息。例如,假设我们有一个航班 destinations 表和一个到达目的地的 flights 表。flights 表包含一个 arrived_at 列,指示航班何时到达目的地。
使用查询构建器的 select 和 addSelect 方法提供的子查询功能,我们可以使用单个查询选择所有 destinations 以及最近到达该目的地的航班名称:
php
use App\Models\Destination;
use App\Models\Flight;
return Destination::addSelect(['last_flight' => Flight::select('name')
->whereColumn('destination_id', 'destinations.id')
->orderByDesc('arrived_at')
->limit(1)
])->get();子查询排序
此外,查询构建器的 orderBy 函数支持子查询。继续使用我们的航班示例,我们可以使用此功能根据最后一次航班到达目的地的时间对所有目的地进行排序。同样,这可以在执行单个数据库查询时完成:
php
return Destination::orderByDesc(
Flight::select('arrived_at')
->whereColumn('destination_id', 'destinations.id')
->orderByDesc('arrived_at')
->limit(1)
)->get();检索单个模型 / 聚合
除了检索匹配给定查询的所有记录外,你还可以使用 find、first 或 firstWhere 方法检索单条记录。这些方法不返回模型集合,而是返回单个模型实例:
php
use App\Models\Flight;
// 通过主键检索模型...
$flight = Flight::find(1);
// 检索匹配查询约束的第一个模型...
$flight = Flight::where('active', 1)->first();
// 检索匹配查询约束的第一个模型的替代写法...
$flight = Flight::firstWhere('active', 1);有时你可能希望在未找到结果时执行其他操作。findOr 和 firstOr 方法将返回单个模型实例,或者如果未找到结果,则执行给定的闭包。闭包返回的值将被视为该方法的结果:
php
$flight = Flight::findOr(1, function () {
// ...
});
$flight = Flight::where('legs', '>', 3)->firstOr(function () {
// ...
});未找到异常
有时你可能希望在未找到模型时抛出异常。这在路由或控制器中特别有用。findOrFail 和 firstOrFail 方法将检索查询的第一个结果;但是,如果未找到结果,将抛出 Illuminate\Database\Eloquent\ModelNotFoundException:
php
$flight = Flight::findOrFail(1);
$flight = Flight::where('legs', '>', 3)->firstOrFail();如果 ModelNotFoundException 未被捕获,将自动向客户端发送 404 HTTP 响应:
php
use App\Models\Flight;
Route::get('/api/flights/{id}', function (string $id) {
return Flight::findOrFail($id);
});检索或创建模型
firstOrCreate 方法将尝试使用给定的列/值对来定位数据库记录。如果在数据库中找不到模型,将插入一条由第一个数组参数与可选的第二个数组参数合并后的属性组成的记录。
firstOrNew 方法与 firstOrCreate 类似,将尝试在数据库中定位与给定属性匹配的记录。但是,如果未找到模型,将返回一个新的模型实例。注意,firstOrNew 返回的模型尚未持久化到数据库中。你需要手动调用 save 方法来持久化它:
php
use App\Models\Flight;
// 通过名称检索航班,如果不存在则创建它...
$flight = Flight::firstOrCreate([
'name' => 'London to Paris'
]);
// 通过名称检索航班,或使用 name、delayed 和 arrival_time 属性创建它...
$flight = Flight::firstOrCreate(
['name' => 'London to Paris'],
['delayed' => 1, 'arrival_time' => '11:30']
);
// 通过名称检索航班,或实例化一个新的 Flight 实例...
$flight = Flight::firstOrNew([
'name' => 'London to Paris'
]);
// 通过名称检索航班,或使用 name、delayed 和 arrival_time 属性实例化...
$flight = Flight::firstOrNew(
['name' => 'Tokyo to Sydney'],
['delayed' => 1, 'arrival_time' => '11:30']
);检索聚合
在与 Eloquent 模型交互时,你还可以使用 Laravel 查询构建器提供的 count、sum、max 和其他聚合方法。如你所料,这些方法返回标量值而非 Eloquent 模型实例:
php
$count = Flight::where('active', 1)->count();
$max = Flight::where('active', 1)->max('price');插入和更新模型
插入
当然,使用 Eloquent 时,我们不仅需要从数据库中检索模型。我们还需要插入新记录。值得庆幸的是,Eloquent 使这变得简单。要将新记录插入数据库,你应该实例化一个新的模型实例并在模型上设置属性。然后,在模型实例上调用 save 方法:
php
<?php
namespace App\Http\Controllers;
use App\Models\Flight;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class FlightController extends Controller
{
/**
* Store a new flight in the database.
*/
public function store(Request $request): RedirectResponse
{
// 验证请求...
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
return redirect('/flights');
}
}在此示例中,我们将传入 HTTP 请求中的 name 字段赋值给 App\Models\Flight 模型实例的 name 属性。当我们调用 save 方法时,将在数据库中插入一条记录。模型的 created_at 和 updated_at 时间戳将在调用 save 方法时自动设置,因此无需手动设置。
或者,你可以使用 create 方法通过单个 PHP 语句"保存"新模型。插入的模型实例将由 create 方法返回给你:
php
use App\Models\Flight;
$flight = Flight::create([
'name' => 'London to Paris',
]);但是,在使用 create 方法之前,你需要在模型类上指定 Fillable 或 Guarded 属性。这些属性是必需的,因为所有 Eloquent 模型默认都受到批量赋值漏洞的保护。要了解更多关于批量赋值的信息,请参阅批量赋值文档。
更新
save 方法也可用于更新数据库中已存在的模型。要更新模型,你应该检索它并设置你希望更新的任何属性。然后,你应该调用模型的 save 方法。同样,updated_at 时间戳将自动更新,因此无需手动设置其值:
php
use App\Models\Flight;
$flight = Flight::find(1);
$flight->name = 'Paris to London';
$flight->save();有时,你可能需要更新现有模型或在不存在匹配模型时创建新模型。与 firstOrCreate 方法类似,updateOrCreate 方法会持久化模型,因此无需手动调用 save 方法。
在下面的示例中,如果存在 departure 位置为 Oakland 且 destination 位置为 San Diego 的航班,其 price 和 discounted 列将被更新。如果不存在此类航班,将创建一个新航班,其属性由第一个参数数组与第二个参数数组合并而成:
php
$flight = Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);使用 firstOrCreate 或 updateOrCreate 等方法时,你可能不知道是创建了新模型还是更新了现有模型。wasRecentlyCreated 属性指示模型是否在其当前生命周期中被创建:
php
$flight = Flight::updateOrCreate(
// ...
);
if ($flight->wasRecentlyCreated) {
// 插入了新的航班记录...
}批量更新
也可以对匹配给定查询的模型执行更新。在此示例中,所有 active 且 destination 为 San Diego 的航班将被标记为延误:
php
Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);update 方法期望接收一个列和值对的数组,表示应该更新的列。update 方法返回受影响的行数。
WARNING
通过 Eloquent 发出批量更新时,不会为更新的模型触发 saving、saved、updating 和 updated 模型事件。这是因为在发出批量更新时,模型实际上从未被检索过。
检查属性变更
Eloquent 提供了 isDirty、isClean 和 wasChanged 方法来检查模型的内部状态,并确定其属性自模型最初检索以来的变化情况。
isDirty 方法确定模型的任何属性自检索模型以来是否已更改。你可以传递特定的属性名称或属性数组给 isDirty 方法,以确定任何属性是否"脏"。isClean 方法将确定属性自模型检索以来是否保持不变。此方法也接受可选的属性参数:
php
use App\Models\User;
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false
$user->isDirty(['first_name', 'title']); // true
$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true
$user->isClean(['first_name', 'title']); // false
$user->save();
$user->isDirty(); // false
$user->isClean(); // truewasChanged 方法确定在当前请求周期内模型上次保存时是否有任何属性被更改。如果需要,你可以传递属性名称来查看特定属性是否被更改:
php
$user = User::create([
'first_name' => 'Taylor',
'last_name' => 'Otwell',
'title' => 'Developer',
]);
$user->title = 'Painter';
$user->save();
$user->wasChanged(); // true
$user->wasChanged('title'); // true
$user->wasChanged(['title', 'slug']); // true
$user->wasChanged('first_name'); // false
$user->wasChanged(['first_name', 'title']); // truegetOriginal 方法返回一个包含模型原始属性的数组,无论自检索以来对模型进行了什么更改。如果需要,你可以传递特定的属性名称以获取特定属性的原始值:
php
$user = User::find(1);
$user->name; // John
$user->email; // john@example.com
$user->name = 'Jack';
$user->name; // Jack
$user->getOriginal('name'); // John
$user->getOriginal(); // 原始属性数组...getChanges 方法返回一个包含模型上次保存时已更改属性的数组,而 getPrevious 方法返回一个包含模型上次保存之前原始属性值的数组:
php
$user = User::find(1);
$user->name; // John
$user->email; // john@example.com
$user->update([
'name' => 'Jack',
'email' => 'jack@example.com',
]);
$user->getChanges();
/*
[
'name' => 'Jack',
'email' => 'jack@example.com',
]
*/
$user->getPrevious();
/*
[
'name' => 'John',
'email' => 'john@example.com',
]
*/批量赋值
你可以使用 create 方法通过单个 PHP 语句"保存"新模型。插入的模型实例将由该方法返回:
php
use App\Models\Flight;
$flight = Flight::create([
'name' => 'London to Paris',
]);但是,在使用 create 方法之前,你需要在模型类上指定 Fillable 或 Guarded 属性。这些属性是必需的,因为所有 Eloquent 模型默认都受到批量赋值漏洞的保护。
当用户通过 HTTP 请求传递一个意外的字段,并且该字段更改了你数据库中你未预期的列时,就会发生批量赋值漏洞。例如,恶意用户可能通过 HTTP 请求发送一个 is_admin 参数,然后将其传递给模型的 create 方法,允许用户将自己提升为管理员。
因此,首先你应该定义哪些模型属性你希望使其可批量赋值。你可以使用模型上的 Fillable 属性来实现。例如,让我们将 Flight 模型的 name 属性设为可批量赋值:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
#[Fillable(['name'])]
class Flight extends Model
{
// ...
}一旦你指定了哪些属性是可批量赋值的,你可以使用 create 方法在数据库中插入新记录。create 方法返回新创建的模型实例:
php
$flight = Flight::create(['name' => 'London to Paris']);如果你已经有一个模型实例,可以使用 fill 方法用属性数组填充它:
php
$flight->fill(['name' => 'Amsterdam to Frankfurt']);批量赋值与 JSON 列
赋值 JSON 列时,每个列的可批量赋值键必须在模型的 Fillable 属性中指定。出于安全考虑,Laravel 不支持在使用 Guarded 属性时更新嵌套的 JSON 属性:
php
use Illuminate\Database\Eloquent\Attributes\Fillable;
#[Fillable(['options->enabled'])]
class Flight extends Model
{
// ...
}允许批量赋值
如果你想使所有属性都可批量赋值,可以在模型上使用 Unguarded 属性。如果你选择取消模型保护,你应该特别注意始终手工构造传递给 Eloquent 的 fill、create 和 update 方法的数组:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Unguarded;
use Illuminate\Database\Eloquent\Model;
#[Unguarded]
class Flight extends Model
{
// ...
}批量赋值异常
默认情况下,未包含在 Fillable 属性中的属性在执行批量赋值操作时会被静默丢弃。在生产环境中,这是预期的行为;但在本地开发期间,可能会导致困惑,不清楚为什么模型更改没有生效。
如果你愿意,可以通过调用 preventSilentlyDiscardingAttributes 方法来指示 Laravel 在尝试填充不可填充属性时抛出异常。通常,应在应用程序的 AppServiceProvider 类的 boot 方法中调用此方法:
php
use Illuminate\Database\Eloquent\Model;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Model::preventSilentlyDiscardingAttributes($this->app->isLocal());
}Upserts
Eloquent 的 upsert 方法可用于在单个原子操作中更新或创建记录。该方法的第一个参数由要插入或更新的值组成,第二个参数列出唯一标识关联表中记录的列。该方法的第三个也是最后一个参数是一个列数组,如果数据库中已存在匹配的记录,则应更新这些列。如果模型启用了时间戳,upsert 方法将自动设置 created_at 和 updated_at 时间戳:
php
Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], uniqueBy: ['departure', 'destination'], update: ['price']);WARNING
除 SQL Server 外的所有数据库都要求 upsert 方法的第二个参数中的列具有"主"或"唯一"索引。此外,MariaDB 和 MySQL 数据库驱动程序会忽略 upsert 方法的第二个参数,始终使用表的"主"和"唯一"索引来检测现有记录。
删除模型
要删除模型,可以在模型实例上调用 delete 方法:
php
use App\Models\Flight;
$flight = Flight::find(1);
$flight->delete();通过主键删除现有模型
在上面的示例中,我们在调用 delete 方法之前从数据库中检索了模型。但是,如果你知道模型的主键,可以通过调用 destroy 方法来删除模型而无需显式检索它。除了接受单个主键外,destroy 方法还将接受多个主键、主键数组或主键集合:
php
Flight::destroy(1);
Flight::destroy(1, 2, 3);
Flight::destroy([1, 2, 3]);
Flight::destroy(collect([1, 2, 3]));如果你使用的是软删除模型,可以通过 forceDestroy 方法永久删除模型:
php
Flight::forceDestroy(1);WARNING
destroy 方法会逐个加载每个模型并调用 delete 方法,以便为每个模型正确调度 deleting 和 deleted 事件。
使用查询删除模型
当然,你可以构建 Eloquent 查询来删除所有匹配查询条件的模型。在此示例中,我们将删除所有标记为非活跃的航班。与批量更新一样,批量删除不会为被删除的模型调度模型事件:
php
$deleted = Flight::where('active', 0)->delete();要删除表中的所有模型,应该执行不添加任何条件的查询:
php
$deleted = Flight::query()->delete();WARNING
通过 Eloquent 执行批量删除语句时,不会为被删除的模型调度 deleting 和 deleted 模型事件。这是因为在执行删除语句时,模型实际上从未被检索过。
软删除
除了从数据库中实际移除记录外,Eloquent 还可以"软删除"模型。当模型被软删除时,它们不会从数据库中实际移除。而是在模型上设置一个 deleted_at 属性,指示模型被"删除"的日期和时间。要为模型启用软删除,请将 Illuminate\Database\Eloquent\SoftDeletes trait 添加到模型:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Flight extends Model
{
use SoftDeletes;
}NOTE
SoftDeletes trait 会自动将 deleted_at 属性转换为 DateTime / Carbon 实例。
你还应该将 deleted_at 列添加到数据库表中。Laravel Schema 构建器包含一个创建此列的辅助方法:
php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('flights', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('flights', function (Blueprint $table) {
$table->dropSoftDeletes();
});现在,当你在模型上调用 delete 方法时,deleted_at 列将被设置为当前日期和时间。但是,模型的数据库记录将保留在表中。查询使用软删除的模型时,软删除的模型将自动从所有查询结果中排除。
要确定给定的模型实例是否已被软删除,可以使用 trashed 方法:
php
if ($flight->trashed()) {
// ...
}恢复软删除的模型
有时你可能希望"取消删除"一个软删除的模型。要恢复软删除的模型,可以在模型实例上调用 restore 方法。restore 方法将模型的 deleted_at 列设置为 null:
php
$flight->restore();你也可以在查询中使用 restore 方法来恢复多个模型。同样,与其他"批量"操作一样,这不会为被恢复的模型调度任何模型事件:
php
Flight::withTrashed()
->where('airline_id', 1)
->restore();restore 方法也可以在构建关联关系查询时使用:
php
$flight->history()->restore();永久删除模型
有时你可能需要从数据库中真正移除模型。你可以使用 forceDelete 方法从数据库表中永久移除软删除的模型:
php
$flight->forceDelete();你也可以在构建 Eloquent 关联关系查询时使用 forceDelete 方法:
php
$flight->history()->forceDelete();查询软删除的模型
包含软删除的模型
如上所述,软删除的模型将自动从查询结果中排除。但是,你可以通过在查询上调用 withTrashed 方法来强制将软删除的模型包含在查询结果中:
php
use App\Models\Flight;
$flights = Flight::withTrashed()
->where('account_id', 1)
->get();withTrashed 方法也可以在构建关联关系查询时调用:
php
$flight->history()->withTrashed()->get();仅检索软删除的模型
onlyTrashed 方法将仅检索软删除的模型:
php
$flights = Flight::onlyTrashed()
->where('airline_id', 1)
->get();修剪模型
有时你可能希望定期删除不再需要的模型。为此,你可以将 Illuminate\Database\Eloquent\Prunable 或 Illuminate\Database\Eloquent\MassPrunable trait 添加到你希望定期修剪的模型中。将其中一个 trait 添加到模型后,实现一个 prunable 方法,该方法返回一个 Eloquent 查询构建器,用于解析不再需要的模型:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
class Flight extends Model
{
use Prunable;
/**
* Get the prunable model query.
*/
public function prunable(): Builder
{
return static::where('created_at', '<=', now()->minus(months: 1));
}
}将模型标记为 Prunable 时,你还可以在模型上定义一个 pruning 方法。此方法将在模型被删除之前调用。此方法可用于在模型从数据库中永久移除之前删除与模型关联的任何额外资源,例如存储的文件:
php
/**
* Prepare the model for pruning.
*/
protected function pruning(): void
{
// ...
}配置可修剪模型后,你应该在应用程序的 routes/console.php 文件中调度 model:prune Artisan 命令。你可以自由选择此命令应运行的适当间隔:
php
use Illuminate\Support\Facades\Schedule;
Schedule::command('model:prune')->daily();在幕后,model:prune 命令将自动检测应用程序 app/Models 目录中的"可修剪"模型。如果你的模型位于不同的位置,可以使用 --model 选项来指定模型类名:
php
Schedule::command('model:prune', [
'--model' => [Address::class, Flight::class],
])->daily();如果你希望在修剪所有其他检测到的模型时排除某些模型,可以使用 --except 选项:
php
Schedule::command('model:prune', [
'--except' => [Address::class, Flight::class],
])->daily();你可以通过使用 --pretend 选项执行 model:prune 命令来测试你的 prunable 查询。在模拟运行时,model:prune 命令将简单地报告如果命令实际运行将修剪多少条记录:
shell
php artisan model:prune --pretendWARNING
如果软删除的模型匹配可修剪查询,它们将被永久删除(forceDelete)。
批量修剪
当模型标记了 Illuminate\Database\Eloquent\MassPrunable trait 时,模型将使用批量删除查询从数据库中删除。因此,pruning 方法将不会被调用,deleting 和 deleted 模型事件也不会被调度。这是因为模型在删除之前实际上从未被检索过,从而使修剪过程更加高效:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\MassPrunable;
class Flight extends Model
{
use MassPrunable;
/**
* Get the prunable model query.
*/
public function prunable(): Builder
{
return static::where('created_at', '<=', now()->minus(months: 1));
}
}复制模型
你可以使用 replicate 方法创建现有模型实例的未保存副本。当你有共享许多相同属性的模型实例时,此方法特别有用:
php
use App\Models\Address;
$shipping = Address::create([
'type' => 'shipping',
'line_1' => '123 Example Street',
'city' => 'Victorville',
'state' => 'CA',
'postcode' => '90001',
]);
$billing = $shipping->replicate()->fill([
'type' => 'billing'
]);
$billing->save();要从复制到新模型的属性中排除一个或多个属性,可以向 replicate 方法传递一个数组:
php
$flight = Flight::create([
'destination' => 'LAX',
'origin' => 'LHR',
'last_flown' => '2020-03-04 11:00:00',
'last_pilot_id' => 747,
]);
$flight = $flight->replicate([
'last_flown',
'last_pilot_id'
]);查询作用域
全局作用域
全局作用域允许你为给定模型的所有查询添加约束。Laravel 自己的软删除功能利用全局作用域仅从数据库中检索"未删除"的模型。编写你自己的全局作用域可以提供一种方便、简单的方式来确保给定模型的每个查询都接收到某些约束。
生成作用域
要生成新的全局作用域,可以调用 make:scope Artisan 命令,该命令会将生成的作用域放在应用程序的 app/Models/Scopes 目录中:
shell
php artisan make:scope AncientScope编写全局作用域
编写全局作用域很简单。首先,使用 make:scope 命令生成一个实现 Illuminate\Database\Eloquent\Scope 接口的类。Scope 接口要求你实现一个方法:apply。apply 方法可以根据需要向查询添加 where 约束或其他类型的子句:
php
<?php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class AncientScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
$builder->where('created_at', '<', now()->minus(years: 2000));
}
}NOTE
如果你的全局作用域正在向查询的 select 子句添加列,你应该使用 addSelect 方法而不是 select。这将防止无意中替换查询的现有 select 子句。
应用全局作用域
要将全局作用域分配给模型,只需在模型上放置 ScopedBy 属性:
php
<?php
namespace App\Models;
use App\Models\Scopes\AncientScope;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
#[ScopedBy([AncientScope::class])]
class User extends Model
{
//
}或者,你可以通过覆盖模型的 booted 方法并调用模型的 addGlobalScope 方法来手动注册全局作用域。addGlobalScope 方法接受你的作用域实例作为其唯一参数:
php
<?php
namespace App\Models;
use App\Models\Scopes\AncientScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::addGlobalScope(new AncientScope);
}
}将上面示例中的作用域添加到 App\Models\User 模型后,调用 User::all() 方法将执行以下 SQL 查询:
sql
select * from `users` where `created_at` < 0021-02-18 00:00:00匿名全局作用域
Eloquent 还允许你使用闭包定义全局作用域,这对于不需要单独类的简单作用域特别有用。使用闭包定义全局作用域时,你应该提供一个自己选择的作用域名称作为 addGlobalScope 方法的第一个参数:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::addGlobalScope('ancient', function (Builder $builder) {
$builder->where('created_at', '<', now()->minus(years: 2000));
});
}
}移除全局作用域
如果你想为给定查询移除全局作用域,可以使用 withoutGlobalScope 方法。此方法接受全局作用域的类名作为其唯一参数:
php
User::withoutGlobalScope(AncientScope::class)->get();或者,如果你使用闭包定义了全局作用域,应该传递你分配给全局作用域的字符串名称:
php
User::withoutGlobalScope('ancient')->get();如果你想移除多个甚至所有查询的全局作用域,可以使用 withoutGlobalScopes 和 withoutGlobalScopesExcept 方法:
php
// 移除所有全局作用域...
User::withoutGlobalScopes()->get();
// 移除部分全局作用域...
User::withoutGlobalScopes([
FirstScope::class, SecondScope::class
])->get();
// 移除除给定作用域之外的所有全局作用域...
User::withoutGlobalScopesExcept([
SecondScope::class,
])->get();局部作用域
局部作用域允许你定义可以在整个应用程序中轻松重用的常见查询约束集。例如,你可能需要频繁检索所有被视为"热门"的用户。要定义作用域,请将 Scope 属性添加到 Eloquent 方法。
作用域应始终返回相同的查询构建器实例或 void:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Scope a query to only include popular users.
*/
#[Scope]
protected function popular(Builder $query): void
{
$query->where('votes', '>', 100);
}
/**
* Scope a query to only include active users.
*/
#[Scope]
protected function active(Builder $query): void
{
$query->where('active', 1);
}
}使用局部作用域
一旦定义了作用域,你可以在查询模型时调用作用域方法。你甚至可以链式调用各种作用域:
php
use App\Models\User;
$users = User::popular()->active()->orderBy('created_at')->get();通过 or 查询运算符组合多个 Eloquent 模型作用域可能需要使用闭包来实现正确的逻辑分组:
php
$users = User::popular()->orWhere(function (Builder $query) {
$query->active();
})->get();但是,由于这可能很麻烦,Laravel 提供了一个"高阶" orWhere 方法,允许你流畅地链式调用作用域而无需使用闭包:
php
$users = User::popular()->orWhere->active()->get();动态作用域
有时你可能希望定义一个接受参数的作用域。首先,只需将额外的参数添加到作用域方法的签名中。作用域参数应在 $query 参数之后定义:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Scope a query to only include users of a given type.
*/
#[Scope]
protected function ofType(Builder $query, string $type): void
{
$query->where('type', $type);
}
}一旦将预期的参数添加到作用域方法的签名中,你可以在调用作用域时传递参数:
php
$users = User::ofType('admin')->get();待定属性
如果你想使用作用域创建具有与约束作用域相同属性的模型,可以在构建作用域查询时使用 withAttributes 方法:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Scope the query to only include drafts.
*/
#[Scope]
protected function draft(Builder $query): void
{
$query->withAttributes([
'hidden' => true,
]);
}
}withAttributes 方法将使用给定的属性向查询添加 where 条件,并且还将给定的属性添加到通过该作用域创建的任何模型中:
php
$draft = Post::draft()->create(['title' => 'In Progress']);
$draft->hidden; // true要指示 withAttributes 方法不向查询添加 where 条件,可以将 asConditions 参数设置为 false:
php
$query->withAttributes([
'hidden' => true,
], asConditions: false);比较模型
有时你可能需要确定两个模型是否"相同"。is 和 isNot 方法可用于快速验证两个模型是否具有相同的主键、表和数据库连接:
php
if ($post->is($anotherPost)) {
// ...
}
if ($post->isNot($anotherPost)) {
// ...
}使用 belongsTo、hasOne、morphTo 和 morphOne 关联关系时,is 和 isNot 方法也可用。当你想比较相关模型而不发出查询来检索该模型时,此方法特别有用:
php
if ($post->author()->is($user)) {
// ...
}事件
NOTE
想要将 Eloquent 事件直接广播到客户端应用程序?请查看 Laravel 的模型事件广播。
Eloquent 模型会调度多个事件,允许你钩入模型生命周期中的以下时刻:retrieved、creating、created、updating、updated、saving、saved、deleting、deleted、trashed、forceDeleting、forceDeleted、restoring、restored 和 replicating。
当从数据库中检索现有模型时,将调度 retrieved 事件。当新模型第一次保存时,将调度 creating 和 created 事件。当现有模型被修改并调用 save 方法时,将调度 updating / updated 事件。当模型被创建或更新时,将调度 saving / saved 事件——即使模型的属性没有被更改。以 -ing 结尾的事件名称在对模型的任何更改被持久化之前调度,而以 -ed 结尾的事件在更改被持久化之后调度。
要开始监听模型事件,请在 Eloquent 模型上定义一个 $dispatchesEvents 属性。此属性将 Eloquent 模型生命周期的各个点映射到你自己的事件类。每个模型事件类应期望通过其构造函数接收受影响模型的实例:
php
<?php
namespace App\Models;
use App\Events\UserDeleted;
use App\Events\UserSaved;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The event map for the model.
*
* @var array<string, string>
*/
protected $dispatchesEvents = [
'saved' => UserSaved::class,
'deleted' => UserDeleted::class,
];
}定义和映射 Eloquent 事件后,你可以使用事件监听器来处理事件。
WARNING
通过 Eloquent 发出批量更新或删除查询时,不会为受影响的模型调度 saved、updated、deleting 和 deleted 模型事件。这是因为在执行批量更新或删除时,模型实际上从未被检索过。
使用闭包
你可以注册在各种模型事件被调度时执行的闭包,而不是使用自定义事件类。通常,你应该在模型的 booted 方法中注册这些闭包:
php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::created(function (User $user) {
// ...
});
}
}如果需要,你可以在注册模型事件时使用可排队匿名事件监听器。这将指示 Laravel 使用应用程序的队列在后台执行模型事件监听器:
php
use function Illuminate\Events\queueable;
static::created(queueable(function (User $user) {
// ...
}));观察者
定义观察者
如果你在给定模型上监听许多事件,可以使用观察者将所有监听器分组到一个类中。观察者类的方法名反映了你想要监听的 Eloquent 事件。每个方法都接收受影响的模型作为其唯一参数。make:observer Artisan 命令是创建新观察者类最简单的方式:
shell
php artisan make:observer UserObserver --model=User此命令将把新观察者放在你的 app/Observers 目录中。如果此目录不存在,Artisan 将为你创建它。你的新观察者将如下所示:
php
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
// ...
}
/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
// ...
}
/**
* Handle the User "deleted" event.
*/
public function deleted(User $user): void
{
// ...
}
/**
* Handle the User "restored" event.
*/
public function restored(User $user): void
{
// ...
}
/**
* Handle the User "forceDeleted" event.
*/
public function forceDeleted(User $user): void
{
// ...
}
}要注册观察者,你可以在对应模型上放置 ObservedBy 属性:
php
use App\Observers\UserObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
//
}或者,你可以通过在希望观察的模型上调用 observe 方法来手动注册观察者。你可以在应用程序的 AppServiceProvider 类的 boot 方法中注册观察者:
php
use App\Models\User;
use App\Observers\UserObserver;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
User::observe(UserObserver::class);
}NOTE
观察者还可以监听其他事件,例如 saving 和 retrieved。这些事件在事件文档中描述。
观察者与数据库事务
当模型在数据库事务中被创建时,你可能希望指示观察者仅在数据库事务提交后执行其事件处理程序。你可以通过在观察者上实现 ShouldHandleEventsAfterCommit 接口来实现这一点。如果没有正在进行的数据库事务,事件处理程序将立即执行:
php
<?php
namespace App\Observers;
use App\Models\User;
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
class UserObserver implements ShouldHandleEventsAfterCommit
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
// ...
}
}静默事件
你可能偶尔需要临时"静默"模型触发的所有事件。你可以使用 withoutEvents 方法来实现。withoutEvents 方法接受一个闭包作为其唯一参数。在此闭包内执行的任何代码都不会调度模型事件,并且闭包返回的任何值都将由 withoutEvents 方法返回:
php
use App\Models\User;
$user = User::withoutEvents(function () {
User::findOrFail(1)->delete();
return User::find(2);
});不触发事件保存单个模型
有时你可能希望"保存"给定模型而不调度任何事件。你可以使用 saveQuietly 方法来实现:
php
$user = User::findOrFail(1);
$user->name = 'Victoria Faith';
$user->saveQuietly();你也可以在不调度任何事件的情况下"更新"、"删除"、"软删除"、"恢复"和"复制"给定模型:
php
$user->deleteQuietly();
$user->forceDeleteQuietly();
$user->restoreQuietly();