jeudi 24 février 2022

Laravel 5.2: How to convert raw query to eloquent?

Here's the raw query.

$past_period_months = Carbon::now()->subMonths(6);

$messages = DB::select(DB::raw("SELECT COUNT(*) AS count FROM messages as m WHERE conversation_id IN ( SELECT id FROM conversations WHERE (user_one = {$id} AND delete_user_one = 0 AND (m.created_at > user_one_convo_deleted_at OR  user_one_convo_deleted_at IS NULL) AND (m.created_at > user_one_msgs_deleted_at OR user_one_msgs_deleted_at IS NULL)) OR (user_two = {$id} AND delete_user_two = 0 AND (m.created_at > user_two_convo_deleted_at OR user_two_convo_deleted_at IS NULL) AND (m.created_at > user_two_msgs_deleted_at OR user_two_msgs_deleted_at IS NULL) ) ) AND is_seen = 0 AND user_id <> {$id} AND user_id NOT IN ( SELECT user_id FROM deactivateaccount ) AND ( user_id NOT IN ( SELECT id FROM users WHERE flag = 0 AND id = user_id ) OR user_id IN ( SELECT id FROM users WHERE membershiptype IN ('admin','premium','free-premium','diamond') AND id = user_id ) ) AND (hard_delete = 0 OR hard_delete IS NULL) AND (deleted_from_receiver = 0 OR deleted_from_receiver IS NULL)  AND (isflaged IS NULL OR isflaged != 1) AND m.created_at >= '{$past_period_months}' ORDER BY created_at DESC"));

return $messages[0]->count;

Now, how do I convert this to eloquent?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire