关键基础上的多维唯一记录

时间:2018-07-21 17:19:24

标签: php database laravel

我有这个数组,我只想基于键user_to

获得唯一记录
{
  "status": true,
  "result": {
    "unread_chat": 1,
    "chatlist": [
      {
        "id": 769,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 754,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 753,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 749,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 748,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 746,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 741,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 740,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 739,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 738,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 735,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 733,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 732,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 729,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 727,
        "user_id": 211,
        "user_to": 39,
        "chats": "hello",
        "is_read": false,
        "created_at": "2018-07-21 17:36:13",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 726,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 725,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": true,

        "unread_msg": 0
      },
      {
        "id": 679,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": true,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 678,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": true,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      },
      {
        "id": 677,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": true,
        "created_at": "2018-07-21 23:31:37",
        "online": false,

        "unread_msg": 0
      }
    ]
  }
}

下面是我使用的代码。

    $messages = UserAdvertiserMessages::where(['user_id' => $user_id])
            ->orderBy('created_at', 'desc')
            ->skip($offset * $limit)
            ->take($limit)
            ->get();

如果我使用->unique('user_to'),则可以得到正确的结果,但是它添加了索引。这样的代码才能获得正确的结果

$messages = UserAdvertiserMessages::where(['user_id' => $user_id])
        ->orderBy('created_at', 'desc')
        ->skip($offset * $limit)
        ->take($limit)
        ->get()->unique('user_to');

输出

{
  "status": true,
  "result": {
    "unread_chat": 1,
    "chatlist": {
      "0": {
        "id": 769,
        "user_id": 211,
        "user_to": 210,
        "chats": "There?",
        "is_read": false,
        "created_at": "2018-07-21 23:31:37",
        "online": false,
        "unread_msg": 0
      },
      "1": {
        "id": 754,
        "user_id": 211,
        "user_to": 226,
        "chats": "yes",
        "is_read": true,
        "created_at": "2018-07-21 22:17:21",
        "online": false,

        "unread_msg": 0
      },
      "14": {
        "id": 727,
        "user_id": 211,
        "user_to": 39,
        "chats": "hello",
        "is_read": false,
        "created_at": "2018-07-21 17:36:13",
        "online": false,

        "unread_msg": 0
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试使用此 distinct()

add_action('wp_head', function (){
  ?>
  <?php 
    if ( is_single() ) {
    echo get_post_meta( get_the_ID(), 'meta-head', true );  
  $postTags = get_the_tags();
  $tagNames = array();
  foreach($postTags as $tag) {
    $tagNames[] = $tag->name;          
      }
?>
<meta name="keywords" content="<?php echo implode($tagNames,","); ?>" />
  <?php
    }