如何在Laravel 4中使用Eloquent Model增加列

Abi*_*hek 3 laravel eloquent laravel-4

我不确定如何使用Laravel 4中的Eloquent Model增加列中的值?这就是我目前所拥有的,我不确定这是多么正确.

$visitor = Visitor::where('token','=','sometoken')->first();
if(isset($visitor)){
    $visitor->increment('totalvisits');
}else{
    Visitor::create(array(
    'token'=>'sometoken',
    'totalvisits'=>0
    ));
}
Run Code Online (Sandbox Code Playgroud)

使用Query Builder,我们可以使用它

DB::table('visitors')->increment('totalvisits');
Run Code Online (Sandbox Code Playgroud)

Abi*_*hek 22

看起来我发布的代码毕竟有效

$visitor = Visitor::where('token','=','sometoken')->first();
if(isset($visitor)){
    $visitor->increment('totalvisits');
}else{
    Visitor::create(array(
    'token'=>'sometoken',
    'totalvisits'=>0
    ));
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*wis 5

几周前修复之前,increment方法实际上落到了查询构建器,并且将在整个表上调用,这是不合需要的.

现在,调用incrementdecrement在模型实例上仅对该模型实例执行操作.