简体   繁体   English

使用laravel将两行mysql合并为一

[英]Merge two mysql rows into one using laravel

I have a table named messages that has the structure as follow: 我有一个名为messages的表,其结构如下:

id | sender_id | receiver_id | message | sent_at

I have made a system that whenever a user selects a particular user the messages that are sent by or received by him are shown in the page in proper manner. 我已经建立了一个系统,每当用户选择一个特定的用户时,该页面上就会以适当的方式显示由他发送或接收的消息。 For that I have the following code that does the thing: 为此,我有以下代码可以完成该任务:

<script type="text/javascript">
function showMessages(elem){
    let id = $(elem).attr('id');
    let myId = "<?php echo \Session::get('user_id'); ?>";
    console.log(id, myId);
    $.get("/get-messages",
{
    id_no_1: id,
    id_no_2: myId
}, function(data){
    alert(data)
});
}

Here, I get the user_id of the associated user and my own user and perform an AJAX request to the route and the route has: 在这里,我获得了关联用户和我自己的用户的user_id,并对路由执行AJAX请求,并且该路由具有:

Route::get('/get-messages', 'PagesController@getMessages');

And my major function looks like this: 我的主要功能如下所示:

public function getMessages(){
    $id_no_1 = request('id_no_1');
    $id_no_2 = request('id_no_2');
    $messagesSendByOneToTwo = DB::table('messages')
            ->where('sender_id', $id_no_1)
            ->where('receiver_id', $id_no_2)
            ->get();
    // $messagesSendByOneToTwo = json_encode($messagesSendByOneToTwo);

    $messagesSendByTwoToOne = DB::table('messages')
            ->where('sender_id', $id_no_2)
            ->where('receiver_id', $id_no_1)
            ->get();
    // $messagesSendByOneToTwo = json_encode($messagesSendByTwoToOne);

    $allMessages = array_merge($messagesSendByOneToTwo, $messagesSendByTwoToOne);

    $allMessages = json_encode($allMessages);

    echo $allMessages;


}

There I have two variables and I want to merge the two variable into a single variable. 那里有两个变量,我想将两个变量合并为一个变量。 I tried array_merge(); 我尝试了array_merge(); which I knew wouldn't work because the variables aren't arrays. 我知道这是行不通的,因为变量不是数组。 To this problem, I have the following possible solutions: 对于这个问题,我有以下可能的解决方案:

  • Merge the two variables into one and perform things with it. 将两个变量合并为一个并执行操作。 (But, I don't know how to do that. (但是,我不知道该怎么做。
  • Send two variables as the response to the JavaScript function handling the response (which I have never heard of). 发送两个变量作为对响应的JavaScript函数的响应(我从未听说过)。
  • Returning to a new view with the variables (which doesn't seem to be a good idea) So, what should I do to solve this problem? 返回到带有变量的新视图(这似乎不是一个好主意)那么,我应该怎么做才能解决此问题? The best way would be to merge the variables into a new one so what should I do here? 最好的方法是将变量合并到一个新变量中,那么我应该在这里做什么?

There's no reason for you to do two queries to begin with: 您没有理由先做两个查询:

public function getMessages(){
    $id_no_1 = request('id_no_1');
    $id_no_2 = request('id_no_2');
    $messagesSent = DB::table('messages')
        ->whereIn('sender_id', [ $id_no_1, $id_no_2 ])
        ->whereIn('receiver_id', [ $id_no_1, $id_no_2 ])
        ->get();


    return response()->json($messagesSent); //This is better than echoing json   
}

If you want an array you can try following code to get array instead of collection. 如果需要数组,可以尝试以下代码获取数组而不是集合。

messagesSendByOneToTwo = DB::table('messages')
            ->where('sender_id', $id_no_1)
            ->where('receiver_id', $id_no_2)
            ->get()->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM