Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将此SQL查询转换为laravel eloquent或查询生成器_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 如何将此SQL查询转换为laravel eloquent或查询生成器

Php 如何将此SQL查询转换为laravel eloquent或查询生成器,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,这是我的聊天表的表结构 Schema::create('chats', function (Blueprint $table) { $table->bigIncrements('id'); $table->enum('block', ['no', 'yes'])->default('no'); $table->timestamps();

这是我的聊天表的表结构

    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');
            });
这是我的聊天室信息表

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();
给你

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

它的结果和你上面写的查询结果一样吗?我的意思是这个查询选择U.id,C.id,U.name,U.email FROM users as U,chats as C,chat_messages as M,当C.user_one='2'时,然后C.user_two=U.id当C.user_two='2'时,然后C.user_one=U.id结束,C.id=M.chat_id和(C.user_one='2'或C.user_two='2'))按C.id排序描述它的结果是否与您上面写的查询相同?我的意思是,此查询选择U.id、C.id、U.name、U.email作为U、chats作为C、chat作为M,当C.user_one='2'时,C.user_two=U.id当C.user_two='2'时,然后C.user_one=U.id结束,C.id=M.chat_id和(C.user_one='2'或C.user_two='2'))按C.id描述订购