0

I'm trying to count unread messages in laravel app, however my "last_viewed" column contains "raw" time in seconds stored in "last_viewed (int) 11" and my "created_at" contains mysql timestamp so I can't compare them in mysql statement. Here is how I compare them, but it's kind of spaghetti

class Helper {

	public static function count_unread_messages()
	{
		        $result = Conversation::join('messages','messages.conversation_id','=','conversation.id')
		                ->join('conversation_members','conversation_members.conversation_id','=','conversation.id')
		                ->select('conversation_members.last_viewed as last_viewed',
		                	'messages.created_at as created_at')
		                ->where('conversation_members.user_id','=',Auth::user()->id)
		                ->groupBy('messages.id')->get();
		$i = 0;
		foreach ($result as $key) {
			    if (date($key->last_viewed) < date(strtotime($key->created_at))) {
			    	$i++;
			    }			
		}
		return $i;
	}
}

but I'd like to compare inside Mysql statement, like that ->where('last_viewed' ,'<', 'created_at')->count()

Is there a way to change format of a timestamp into seconds inside a mysql statement?

5
  • The function you're looking for is called FROM_UNIXTIME(), see documentation here: dev.mysql.com/doc/refman/5.5/en/…. You're going to have to use DB::raw() statements, by the way. Good luck! :) Commented Jan 26, 2015 at 21:34
  • thank You, but FROM_UNIXTIME() converts seconds into timestamp, is there a function which converts timestamps into seconds? Commented Jan 26, 2015 at 21:45
  • The reverse is called UNIX_TIMESTAMP() - documentation here: dev.mysql.com/doc/refman/5.5/en/…. Although you can compare either seconds or dates directly, it'll result in the same thing. That said, do you really store in timestamp - not in datetime? Commented Jan 26, 2015 at 21:49
  • Yes I store 'created_at' in timestamp(it requiered by laravel 4) and 'last_viewed' in int11 seconds, however when I compare dates it gives me some random results, so I prefer comparing in seconds Commented Jan 26, 2015 at 22:14
  • Fair enough. I'd forgotten Laravel doesn't use datetime. Commented Jan 26, 2015 at 23:17

1 Answer 1

1

I think the best way to solve this is to persist time stamps as carbon instances.

You can do that by setting protected $dates = ['last_viewed', 'created_at']; in your Conversation model.

Now when the data get persisted, since last_viewed and created_at persists as carbon instances and mainly because eloquent support carbon, you can simply achieve

->where('last_viewed', '<', 'created_at')->count() functionality.

Hope this was helpful.

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.