0

I want to add 1 to my previous karma value in my database. Here's what I am doing in my controller:-

 public function response(Request $request, $id)
    {   
        $globalPost = PublicAnswer::find($id);

        if($request->resp == "normal")
        {
          $answered_by = DB::table('users')
        ->where('id', $globalPost->answered_by)
        ->increment('karma', +1);
        
        }
}

Here's my database:-

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('karma')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

If I replace increment function with update this is the error that I am getting:-

Argument 1 passed to Illuminate\Database\Query\Builder::update() must be of the type array, string given,

I cannot see any errors and the karma attribute is null.
can anyone tell me whats wrong with my codes.

2 Answers 2

1
DB::table('users')
        ->where('id', $globalPost->answered_by)

returns a Illuminate\Database\Query\Builder ; Illuminate\Database\Query\Builder has an increment function

public function increment($column, $amount = 1, array $extra = [])
   

and an update function

public function update(array $values)

so you can try

DB::table('users')
        ->where('id', $globalPost->answered_by)
        ->increment('karma');

or this, this would set karma to 1

DB::table('users')
        ->where('id', $globalPost->answered_by)
        ->update(['karma'=>1]);

if you want use update set karma=karma+1

use Illuminate\Database\Query\Expression;
DB::table('users')
        ->where('id', $globalPost->answered_by)
        ->update(['karma'=>new Expression('karma + 1')]);
Sign up to request clarification or add additional context in comments.

5 Comments

Rof That update function you mentioned in your answer wouldn't it just replace the old karma value with the 1 ? I wanna increase the value by 1
@AnonymousChatbox I just check it in tinker, both worked.Both method return the number of affected rows.
my default value was null maybe that's why it's was not working.
@AnonymousChatbox if you really want use update set karma = karma +1 ,you can use use Illuminate\Database\Query\Expression; DB::table('users') ->where('id', $globalPost->answered_by) ->update(['karma'=>new Expression('karma + 1')]);
I just changed my karma form nullable() to default(0) and used increment function it all works fine now. Thanks.
1

According to the docs https://laravel.com/docs/8.x/queries#increment-and-decrement you simply want increment('karma') to increment by 1

The second argument may optionally be passed to control the amount by which the column should be incremented or decremented. You do not need to precede the amount with the plus sign either as increment and decrement mean add or subtract respectively

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.