Laravel 4.2 - 使用 ::firstOrCreate 并确定实际发生了什么?
Laravel 4.2 - using ::firstOrCreate and determining what actually happened?
在 Laravel 4.2 中使用 Model::firstOrCreate
时,有没有办法知道发生了什么,先发生还是创建?
让我解释一下,我的理解是,使用 ::firstOrCreate 创建一条记录,如果它不存在或返回包含参数数组数据的记录值。
所以如果我有这样的东西:
// Auth::user()->id; returns 7
$person = Persons::firstOrCreate(array(
'approver' => 123,
'receiver' => Auth::user()->id,
'plan' => 'gold',
));
我有这些记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
128 | 7 | gold
____________________________________
123 | 7 | platinum
____________________________________
然后将创建一个新记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
128 | 7 | gold
____________________________________
123 | 7 | platinum
____________________________________
123 | 7 | gold
____________________________________
如果我再次运行上面的代码,那么新值将不会被创建,这条记录将被返回
approver | receiver | plan
____________________________________
123 | 7 | gold
____________________________________
但是如果我的代码是这样的
$person = Persons::firstOrCreate(array(
'approver' => 123,
'receiver' => Auth::user()->id
));
将返回这些记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
123 | 7 | platinum
____________________________________
123 | 7 | gold
____________________________________
如果我对 ::firstOrCreate 的理解不正确,请帮助我更好地理解它。
正如我提到的,我很想知道是否有一种方法可以确定是否发生了新记录的创建?
该模型有 wasRecentlyCreated
属性:
$person = Persons::firstOrCreate([
'approver' => 123,
'receiver' => auth()->id(),
'plan' => 'gold',
]);
if ($person->wasRecentlyCreated) {
// $person was created
} else {
// $person was fetched
}
对于旧版本的 Laravel,您可以使用 firstOrNew
实现您想要的:
$person = Persons::firstOrNew([
'approver' => 123,
'receiver' => Auth::user()->id,
'plan' => 'gold',
]);
if (! $person->id) {
$person->save();
// $person was created...
} else {
// $person was fetched...
}
在 Laravel 4.2 中使用 Model::firstOrCreate
时,有没有办法知道发生了什么,先发生还是创建?
让我解释一下,我的理解是,使用 ::firstOrCreate 创建一条记录,如果它不存在或返回包含参数数组数据的记录值。
所以如果我有这样的东西:
// Auth::user()->id; returns 7
$person = Persons::firstOrCreate(array(
'approver' => 123,
'receiver' => Auth::user()->id,
'plan' => 'gold',
));
我有这些记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
128 | 7 | gold
____________________________________
123 | 7 | platinum
____________________________________
然后将创建一个新记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
128 | 7 | gold
____________________________________
123 | 7 | platinum
____________________________________
123 | 7 | gold
____________________________________
如果我再次运行上面的代码,那么新值将不会被创建,这条记录将被返回
approver | receiver | plan
____________________________________
123 | 7 | gold
____________________________________
但是如果我的代码是这样的
$person = Persons::firstOrCreate(array(
'approver' => 123,
'receiver' => Auth::user()->id
));
将返回这些记录
approver | receiver | plan
____________________________________
123 | 7 | silver
____________________________________
123 | 7 | platinum
____________________________________
123 | 7 | gold
____________________________________
如果我对 ::firstOrCreate 的理解不正确,请帮助我更好地理解它。
正如我提到的,我很想知道是否有一种方法可以确定是否发生了新记录的创建?
该模型有 wasRecentlyCreated
属性:
$person = Persons::firstOrCreate([
'approver' => 123,
'receiver' => auth()->id(),
'plan' => 'gold',
]);
if ($person->wasRecentlyCreated) {
// $person was created
} else {
// $person was fetched
}
对于旧版本的 Laravel,您可以使用 firstOrNew
实现您想要的:
$person = Persons::firstOrNew([
'approver' => 123,
'receiver' => Auth::user()->id,
'plan' => 'gold',
]);
if (! $person->id) {
$person->save();
// $person was created...
} else {
// $person was fetched...
}