Laravel 5.0 Eloquent需要查询以在facebook样式中列出用户,帖子和相关评论

时间:2015-11-20 10:27:01

标签: laravel-5

我的表格字段如下:

users :    id  email     first_name    last_name     (rest are there but need only these)
activity_stream :    id  user_id    post          created_at    updated_at  deleted_at
activity_stream_comments : id  post_id  user_id       comment       created_at  updated_at  deleted_at

我需要列出所有用户帖子以及相关评论,就像你在facebook中看到的那样,它会显示各个帖子的帖子评论,所以现在我基本上需要列出所有用户帖子及相关评论。

我正在尝试以下查询,但它只提供在不同集合中也有评论的帖子:

$results = DB::table('activity_stream')
    ->join('users', 'users.id', '=', 'activity_stream.user_id')
    ->join('activity_stream_comments', 'activity_stream.id', '=', 'activity_stream_comments.post_id')
    ->select('activity_stream.*', 'users.first_name','users.last_name', 'activity_stream_comments.comment')
    /*->where('activity_stream.user_id', $user_id)*/
->whereNull('activity_stream.deleted_at')
    ->orderBy('activity_stream.created_at', 'desc')
    ->get();

return response()->json($results);

回报是:

[
  {
    "id": "97",
    "user_id": "30",
    "post": "hi how are you?",
    "created_at": "2015-11-20 04:35:11",
    "updated_at": "2015-11-20 04:35:11",
    "deleted_at": null,
    "first_name": "Abhijan",
    "last_name": "Pal",
    "comment": "test"
  },
  {
    "id": "14",
    "user_id": "49",
    "post": "Test Post",
    "created_at": "2015-11-13 04:51:53",
    "updated_at": "2015-11-13 04:51:53",
    "deleted_at": null,
    "first_name": "Nazir",
    "last_name": "Hussain",
    "comment": "This is my first comment"
  },
  {
    "id": "14",
    "user_id": "49",
    "post": "Test Post",
    "created_at": "2015-11-13 04:51:53",
    "updated_at": "2015-11-13 04:51:53",
    "deleted_at": null,
    "first_name": "Nazir",
    "last_name": "Hussain",
    "comment": "This is my 2nd comment"
  }
]

改进:

$results = User::with('activity_stream.activity_stream_comments')->find($user_id);

return response()->json($results);

在使用关系并使用上述查询时,我得到如下响应:

{
  "id": "49",
  "email": "nazir.2cool@gmail.com",
  "permissions": null,
  "last_login": "2015-11-19 09:32:36",
  "first_name": "Nazir",
  "last_name": "Hussain",
  "created_at": "2015-11-03 12:36:56",
  "updated_at": "2015-11-19 09:32:36",
  "facebook_id": "1051400994880427",
  "facebook_token": "CAAX3UySfMDcBAFs8glgjz9p1Qdt0M1zwRrmZB9y4WkEPOEldIM5BKVURut8pcg9ios6GiZBschXWBKCqM6HdyZCIxkih6Rwh2SSABrzAHZCzGZBqcISDIlUwnNp73zF8PEZAgXKdzhZAQtfjZAeawUlAHTNpt2RyXTPoeH0ETj8jSclIxMSXPxfbDlnmNjoJXVwZD",
  "facebook_profile_url": "",
  "facebook_avatar": "https://graph.facebook.com/v2.5/1051400994880427/picture?width=1920",
  "google_id": "112519651577038840839",
  "google_token": "ya29.MQLD4wmUQSqGyMVENv8mrko-tvsqg3aLibdy5m-KWBp-HqP5liYB8GlWvSC4c1ZXka3YRRgA2dX40Q",
  "google_profile_url": "https://plus.google.com/+NazirHussain91",
  "google_avatar": "https://lh3.googleusercontent.com/-nLaXbUtBV0g/AAAAAAAAAAI/AAAAAAAAAAA/QaM3CzCJxlM/photo.jpg?sz=50",
  "twitter_id": "832857385",
  "twitter_token": "832857385-2PqAa48RffL2Yl7ZNJinBhIawMmH56amI54OBklR",
  "twitter_profile_url": "",
  "twitter_avatar": "http://pbs.twimg.com/profile_images/2626032008/hzd0ynsei7qjpyobaklw.jpeg",
  "role": "author",
  "activity_stream": [
    {
      "id": "14",
      "user_id": "49",
      "post": "Test Post",
      "created_at": "2015-11-13 04:51:53",
      "updated_at": "2015-11-13 04:51:53",
      "deleted_at": null,
      "activity_stream_comments": [
        {
          "id": "1",
          "post_id": "14",
          "user_id": "49",
          "comment": "This is my first comment",
          "created_at": "2015-11-19 10:49:13",
          "updated_at": "2015-11-19 10:49:13",
          "deleted_at": null
        },
        {
          "id": "2",
          "post_id": "14",
          "user_id": "49",
          "comment": "This is my second comment that I edited as well",
          "created_at": "2015-11-19 11:03:01",
          "updated_at": "2015-11-20 11:35:04",
          "deleted_at": null
        },
        {
          "id": "6",
          "post_id": "14",
          "user_id": "30",
          "comment": "This is another test comment",
          "created_at": "-0001-11-30 00:00:00",
          "updated_at": "-0001-11-30 00:00:00",
          "deleted_at": null
        }
      ]
    },
    {
      "id": "24",
      "user_id": "49",
      "post": "This is my first post",
      "created_at": "2015-11-13 06:23:24",
      "updated_at": "2015-11-13 06:23:24",
      "deleted_at": null,
      "activity_stream_comments": []
    }
  ]
}

现在有两个问题:1。如何使用orderBy和2.在帖子和评论级别如何同时获取用户名和用户ID,因为再次查询根本不是一个好主意。

0 个答案:

没有答案