我使用的是 laravel v3.2.12-4,我遇到了递减函数的问题。此方法不是只更新一行,而是影响列中的所有行。我使用 Eloquent,我有一个 many_to_many 关系。
包含递减方法的代码是:
foreach ($ids as $id) {
$indicator = Indicator::find($id);
$tags = $indicator->tags()->get();
foreach ($tags as $tag) {
$indicator->tags()->detach($tag->id);
if ($tag->frequency == 1) {
$tag->delete();
} else {
// I have to made this code to fix the problem with decrement function
// But i want to use decrement
$tag->frequency = $tag->frequency - 1;
$tag->save();
// This dosnt work for me.
// $tag->decrement('frequency');
}
}
$indicator->delete();
}
在模型类指标中,我与这个函数建立了关系:
public function tags()
{
return $this->has_many_and_belongs_to('Tag');
}
在模型类标签中,我与这个函数建立了关系:
public function indicators()
{
return $this->has_many_and_belongs_to('Indicator');
}
好吧,如果我对列进行了更新,这个结果对我来说没问题,但是当我使用递减函数时,这会影响所有行,我不知道这是一个错误还是这个方法的问题。
谢谢。