phplaraveleloquentlaravel-4

How to increment a column using Eloquent Model in Laravel 4


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.

$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

DB::table('visitors')->increment('totalvisits');

Solution

  • 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
        ));
    }