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;
}
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.
Nope, didn't work, same issue :( thinking of trying some kind of event
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 ] );
}
}
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!
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!
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community