jeudi 24 février 2022

Laravel 5.2: Syntax errors after converting raw query to eloquent using converter tool

I've converted the below raw query to eloquent using https://jjlabajo.github.io/SQLtoEloquent/ but there seems to be a couple of syntax errors.

RAW

$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;

ELOQUENT

DB::table("messages as m")
    ->select("count (*) as count")
    ->whereIn("conversation_id", function($query){
    $query->from("conversations")
    ->select("id")
    ->where("(user_one", "=", {$id}]
    ->where("delete_user_one", "=", 0)
    ->where("(m.created_at", ">", user_one_convo_deleted_at)
    ->whereNull("user_one_convo_deleted_at");
    })
    ->where(DB::raw("where_subquery_group_3_)"))
    ->where(DB::raw("(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)"))
    ->where("is_seen", "=", 0)
    ->where("user_id", "<>", {$id})
    ->where(DB::raw("( user_id not in ( select id from users where flag = 0 and id = user_id )"))
    ->whereIn("user_id", function($query){
    $query->from("users")
    ->select("id")
    ->whereIn("membershiptype", "('admin',");
    })
    ->where("id", "=", user_id)
    ->where(DB::raw("(hard_delete = 0 or hard_delete is null)"))
    ->where(DB::raw("(deleted_from_receiver = 0 or deleted_from_receiver is null)"))
    ->where(DB::raw("(isflaged is null or isflaged != 1)"))
    ->where("m.created_at", ">=", {$past_period_months})
    ->orderBy("created_at","desc")
    ->get();

Here's a screenshot of the errors

Why are there errors and how do I fix them? Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire