php - 如何将此 SQL 查询转换为 Laravel Eloquent 或查询生成器

标签 php mysql laravel eloquent

这是我的聊天表的表结构

    Schema::create('chats', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->enum('block', ['no', 'yes'])->default('no');
                $table->timestamps();

                $table->bigInteger('user_one')->unsigned();
                $table->foreign('user_one')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');

                $table->bigInteger('user_two')->unsigned();
                $table->foreign('user_two')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');
            });

这是我的 chat_messages 表

Schema::create('chat_messages', function (Blueprint $table) {
                $table->increments('id');
                $table->enum('viewed', ['no', 'yes'])->default('no');
                $table->string('msg', 1000);
                $table->timestamps();

                $table->bigInteger('chat_id')->unsigned();
                $table->foreign('chat_id')
                        ->references('id')
                        ->on('chats')
                        ->onDelete('cascade');

                $table->bigInteger('user_id')->unsigned();
                $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');
            });

我现在的问题是我想运行这个在 phpmyadmin 中运行良好的查询

SELECT U.id,C.id,U.name,U.email FROM users as U, chats as C, chat_messages as M WHERE CASE WHEN C.user_one = '2' THEN C.user_two = U.id WHEN C.user_two = '2' THEN C.user_one= U.id END AND C.id = M.chat_id AND (C.user_one ='2' OR C.user_two ='2') ORDER BY C.id DESC 

这是我使用 Laravel 查询生成器的上述查询版本,它似乎不起作用

$authUser = Auth::user()->id;
    $chatList = DB::select(DB::raw('U.id, C.id, U.name'))
                ->from(DB::raw('users as U, chats as C, chat_messages as M'))
                ->where(DB::raw('CASE
                    WHEN C.user_one = '.Auth::user()->id.'
                    THEN C.user_two = U.id
                    WHEN C.user_two = '.Auth::user()->id.'
                    THEN C.user_one= U.id END'))
                ->where('C.id', '=', 'M.chat_id')
                ->where(function($query) use ($authUser) {
                    $query->where('C.user_one', '=', $authUser)
                            ->orWhere('C.user_two', '=', $authUser);
                })
                ->orderBy('C.id', 'desc')
                 ->get();

它排除了这个错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'U.id, C.id, U.name' at line 1 (SQL: U.id, C.id, U.name)

我已经尝试了很多方法,但似乎没有用......请我需要你的帮助来弄清楚如何处理这个......谢谢

最佳答案

在这里

    $authUser = Auth::user()->id;
    $chatList = DB::table(DB::raw('users as U, chats as C, chat_messages as M'))
        ->select(DB::raw('U.id, C.id, U.name, U.email'))
        ->whereRaw('CASE
                WHEN C.user_one = '.Auth::user()->id.'
                THEN C.user_two = U.id
                WHEN C.user_two = '.Auth::user()->id.'
                THEN C.user_one= U.id END
                AND C.id = M.chat_id AND 
                (C.user_one = '.$authUser.' OR C.user_two = '.$authUser.')')
        ->orderBy('C.id', 'DESC')
        ->get();

关于php - 如何将此 SQL 查询转换为 Laravel Eloquent 或查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567099/

相关文章:

php - 如何在 View 页面中获取多个值

php - 加入 3 个 mysql 表 php codeigniter

php - Laravel 在某些机器 - 浏览器中返回 302

laravel - 使用成功消息在 laravel 中插入数据后重定向

php - Amfphp、Actionscript 3 数据集成中子数组消失

php - 在 magic_quotes 打开的服务器上 INSERT 失败

php - 如何使用 curl 正确发送和接收 XML?

php - Joomla : how to get the url of a specific Menu itemID?

mysql - 如何更新表数组

mysql - 如何通过组合电子邮件和日期来获取 "unique"用户