Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Try removing public $timestamps = true; from the model and see what happens. I just created a forum and we used this and it worked wonderfully. I don't think removing that from the model is going to help, but it is worth a shot. It would seem soemthing else is going on. What does Helper::checkEdit do?

    public function updateNumViews($topic_id) {
        $post = \ForumTopics::find($topic_id);
        $post->post_views = $post->post_views + 1;
        $post->timestamps = false;
        if ($post->save()) {
            return 1;
        }

        return 0;
    }
0

I had just added that public $timestamps actually, thanks for pointing that out, but that's not it unfortunately.

checkEdit() just checks whether or not the user can edit the post.

Going to try adding the $id to the function and finding it that way. Will update with results.

0

Nope, didn't work, same issue :( thinking of trying some kind of event

0

Seems like event listeners worked:

Event::listen( 'thread.increment', function( $id, $timestamp )
{
    $thread = WallThread::find( $id );
    $thread->updated_at = $timestamp;
    $thread->save();
});
	public function updateViews()
	{
		$this->timestamps = false;
		$this->views = $this->views + 1;
		if ( $this->save() ) {
			Event::fire( 'thread.increment', [ $this->id, $this->updated_at ] );
		}
	}
0

Wow. That is pretty cool. I created an achievement system and I could totally change the way I implemented this how you have this. I love Laravel. I'm pretty new to it, so every time I turn a corner I'm learning something to help me out more!

Good job!

0

I am about to start an acheivement system as well, Event listeners are DEFINITELY the way to go for that :D and yes I have not looked back ever since I came across Laravel, amazing!

0

For increased performance you might want to look at laravel queues to postpone increments and do them in bulk. I also store my view counters in a separate lightweight db table as well. This has really cut down the io demand on my busy servers.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ncovill ncovill Joined 12 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.