Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 318
Abishek Avatar asked Nov 27 '22 07:11

Abishek


1 Answers

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
    ));
}
like image 55
Abishek Avatar answered Dec 04 '22 02:12

Abishek