0

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!

3
  • OMG, yes it does work when omitting "first()". I figured it was something simple. Much appreciated! Commented Jan 22, 2016 at 21:47
  • Haha, you're welcome! Glad you saw my suggestion before I deleted it. It was a hunch, but I wasn't certain. :) In the documentation, ->increment() was always called on the full resultset -- and in the Laravel method definition for increment I saw that increment() returns an int, so it does not support method chaining after it. Commented Jan 22, 2016 at 21:51
  • That's excellent, thanks for the help! Commented Jan 22, 2016 at 21:58

1 Answer 1

1

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

-- Submitted by mmccaff

Sign up to request clarification or add additional context in comments.

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.