如何在 Laravel 4 中使用 Eloquent 模型增加一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16597426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to increment a column using Eloquent Model in Laravel 4
提问by Abishek
I am not sure how to increment the value in a column using Eloquent Model in Laravel 4? This is what I currently have and I am not sure how correct is this.
我不确定如何在 Laravel 4 中使用 Eloquent 模型增加列中的值?这是我目前拥有的,我不确定这有多正确。
$visitor = Visitor::where('token','=','sometoken')->first();
if(isset($visitor)){
$visitor->increment('totalvisits');
}else{
Visitor::create(array(
'token'=>'sometoken',
'totalvisits'=>0
));
}
With Query Builder we could do it using
使用 Query Builder 我们可以使用
DB::table('visitors')->increment('totalvisits');
回答by Abishek
Looks like the code that I posted worked after all
看起来我发布的代码毕竟有效
$visitor = Visitor::where('token','=','sometoken')->first();
if(isset($visitor)){
$visitor->increment('totalvisits');
}else{
Visitor::create(array(
'token'=>'sometoken',
'totalvisits'=>0
));
}
回答by Jason Lewis
Prior to a fix a few weeks agothe increment
method actually fell through to the query builder and would be called on the entire table, which was undesirable.
在几周前修复之前,该increment
方法实际上落入了查询构建器,并将在整个表上调用,这是不可取的。
Now calling increment
or decrement
on a model instance will perform the operation only on that model instance.
现在在模型实例上调用increment
或decrement
将仅在该模型实例上执行操作。
回答by malhal
Laravel 5 now has atomic increment:
Laravel 5 现在具有原子增量:
public function increment($column, $amount = 1, array $extra = [])
{
if (! is_numeric($amount)) {
throw new InvalidArgumentException('Non-numeric value passed to increment method.');
}
$wrapped = $this->grammar->wrap($column);
$columns = array_merge([$column => $this->raw("$wrapped + $amount")], $extra);
return $this->update($columns);
}
which essentially works like:
它的工作原理如下:
Customer::query()
->where('id', $customer_id)
->update([
'loyalty_points' => DB::raw('loyalty_points + 1')
]);
Below is old answer for Laravel 4 where the built-in increment was a seperate select and then update which of course leads to bugs with multiple users:
下面是 Laravel 4 的旧答案,其中内置增量是一个单独的选择,然后更新这当然会导致多个用户的错误:
If you'd like to accurately count your visitors by ensuring the update is atomic then try putting this in your Visitor model:
如果您想通过确保更新是原子性的来准确计算访问者的数量,请尝试将其放入访问者模型中:
public function incrementTotalVisits(){
// increment regardless of the current value in this model.
$this->where('id', $this->id)->update(['totalVisits' => DB::raw('last_insert_id(totalVisits + 1)')]);
//update this model incase we would like to use it.
$this->totalVisits = DB::getPdo()->lastInsertId();
//remove from dirty list to prevent any saves overwriting the newer database value.
$this->syncOriginalAttribute('totalVisits');
//return it because why not
return $this->totalVisits;
}
I'm using it for a change tag system but might work for your needs too.
我将它用于更改标签系统,但也可能满足您的需求。
Does anyone know what to replace the "$this->where('id',$this->id)" with because since dealing with $this Visitor it should be redundant.
有谁知道用什么来替换 "$this->where('id',$this->id)" 因为因为处理 $this Visitor 它应该是多余的。