Matt Pierce
Matt Pierce

Reputation: 805

Laravel Increment Column Value

This has to be a simple fix, but it's not clicking with me. I am simply trying to find a record in my table and increment one column.

    $visits = DB::table('recently_visited')
        ->where('profile_id',$user->id)
        ->where('visitor_id',Auth::user()->id)
        ->first();

This accurately returns the database record as when I dd $visits, here's an example of the output.

{#197 ▼
  +"id": 6
  +"visitor_id": 2
  +"profile_id": 2
  +"times_visited": 6
  +"last_visit": "0000-00-00 00:00:00"
}

But then when I attach the "increments onto it...

    $visits->increment('times_visited');

I get the error...Fatal error: Call to undefined method stdClass::increment()

I have also tried doing the following...

$visits = DB::table('recently_visited')
            ->increment('times_visited')
            ->where('profile_id',$user->id)
            ->where('visitor_id',Auth::user()->id)
            ->first();

But I get...Fatal error: Call to a member function where() on integer

Thanks!

Upvotes: 0

Views: 1622

Answers (1)

Matt Pierce
Matt Pierce

Reputation: 805

Omitting "first()" and chaining "increment()" to the query works.

-- Submitted by mmccaff

Upvotes: 1

Related Questions