将两个查询合并在一起sortByDesc和paginate

时间:2018-11-28 21:20:57

标签: laravel eloquent

我正在构建一个报告系统,用户可以在其中报告违反了要审核站点规则的内容。

表格

帖子

  • id(int)
  • user_id(int)
  • 内容(文字)
  • 标题(文本)
  • created_at

评论

  • id(int)
  • 内容(int)
  • user_id(int)
  • post_id(int)
  • created_at

报告

  • id(int)
  • user_id(int)
  • 内容(文本)
  • reportable_id(int)
  • reportable_type(字符串)

用户

  • id(int)
  • 名称(字符串)

我要做什么

我想做的是同时查询具有报告的帖子和评论,渴望加载报告,将帖子和评论合并在一起,在帖子和评论模型上sortByDesc('created_at'),最后对合并的查询进行分页。

ReportController尝试

public function index()
{

    $posts = Post::whereHas('reports')
    ->with('reports.user')
    ->get();

    $comments  = Comment::whereHas('reports')
    ->with('reports.user', 'post')
    ->get();

    //I have no idea how to paginate this. 
    return $postAndCommentReports = $posts->merge($comments)->sortByDesc('created_at');
}

查询输出

{  
   "0":{  
      "id":14,
      "user_id":5,
      "post_id":13,
      "content":"Nobis possimus temporibus iure molestiae. Impedit optio nobis quae. Sunt ut voluptatem libero sunt similique sit.",
      "created_at":"1984-02-20 04:45:37",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":9,
            "is_handled":0,
            "user_id":10,
            "content":"Rule 1 broken on this comment",
            "reportable_id":14,
            "reportable_type":"App\\Comment",
            "created_at":"2017-08-07 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":10,
               "name":"Neva Daugherty",
               "email":"hadley17@example.com",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ],
      "post":{  
         "id":13,
         "user_id":10,
         "title":"Impedit voluptas incidunt autem a cum voluptatibus alias.",
         "body":"Vel libero corrupti aperiam harum aut. Rerum accusamus et repellendus ipsa quo porro qui nemo. Velit ratione ut ipsam natus ullam repellat sint iure. Cupiditate quis cupiditate voluptas minus nemo nobis quia.",
         "created_at":"1983-04-02 17:55:56",
         "updated_at":"2018-11-28 20:36:19"
      }
   },
   "1":{  
      "id":3,
      "user_id":20,
      "title":"Dolore dicta similique sapiente consequuntur deserunt voluptas vel.",
      "body":"Provident ut aut minima iure veniam. Minus placeat temporibus sit. Quos aliquid natus rerum.",
      "created_at":"1996-01-14 01:25:34",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":2,
            "is_handled":0,
            "user_id":1,
            "content":"This is a horrible post",
            "reportable_id":3,
            "reportable_type":"App\\Post",
            "created_at":"2017-08-04 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":1,
               "name":"Keegan Schultz",
               "email":"breanne09@example.com",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ]
   },
   "2":{  
      "id":10,
      "user_id":13,
      "title":"Eaque amet sed quia quo.",
      "body":"Rem ut illum asperiores nostrum. Aut ipsam ut qui quisquam quis iure. Itaque incidunt earum velit est est hic reprehenderit pariatur. Rerum ipsum fuga incidunt omnis repellat.",
      "created_at":"1971-07-08 05:14:57",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":5,
            "is_handled":0,
            "user_id":15,
            "content":"Rule 2 broken on this post",
            "reportable_id":10,
            "reportable_type":"App\\Post",
            "created_at":"2017-08-10 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":15,
               "name":"Fritz Champlin",
               "email":"beth.dach@example.org",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ]
   },
   "3":{  
      "id":31,
      "user_id":1,
      "post_id":17,
      "content":"Dolores voluptatem eveniet animi tempora et accusantium. Aut dolores molestias reiciendis in accusamus ex. Ab hic quia non corrupti fuga.",
      "created_at":"2001-05-19 22:21:47",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":7,
            "is_handled":0,
            "user_id":17,
            "content":"This is a horrible comment",
            "reportable_id":31,
            "reportable_type":"App\\Comment",
            "created_at":"2017-08-03 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":17,
               "name":"Lacey Walter",
               "email":"emard.rene@example.org",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ],
      "post":{  
         "id":17,
         "user_id":9,
         "title":"Quia sit aut incidunt nulla minima odit nesciunt.",
         "body":"Qui aliquam odio aut eum quaerat deserunt rerum. Molestiae veritatis eaque aut excepturi et. Enim perferendis eaque asperiores laudantium occaecati. Amet fugit cumque ut et reprehenderit maiores deleniti.",
         "created_at":"2007-06-14 12:01:36",
         "updated_at":"2018-11-28 20:36:19"
      }
   },
   "4":{  
      "id":34,
      "user_id":17,
      "post_id":12,
      "content":"Deleniti dolorem esse exercitationem illum quidem. Quia inventore molestias tenetur officiis. Distinctio facilis corporis omnis molestiae. Ut corporis culpa error atque consequatur.",
      "created_at":"1994-04-11 09:26:17",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":10,
            "is_handled":0,
            "user_id":6,
            "content":"Rule 2 broken on this comment",
            "reportable_id":34,
            "reportable_type":"App\\Comment",
            "created_at":"2017-08-09 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":6,
               "name":"Bernita Hahn",
               "email":"tiffany99@example.org",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ],
      "post":{  
         "id":12,
         "user_id":13,
         "title":"Placeat ad nihil veritatis.",
         "body":"Dolor nam tempore suscipit quia ut. Ab exercitationem hic ut in ut temporibus ab. Et voluptatum dolores expedita enim ullam quia. Qui optio quia exercitationem quo molestias a minus.",
         "created_at":"2010-10-02 08:36:20",
         "updated_at":"2018-11-28 20:36:19"
      }
   },
   "5":{  
      "id":49,
      "user_id":20,
      "post_id":8,
      "content":"Quis commodi a aut aut. Assumenda sit officiis placeat ipsam sit veritatis vel qui. Corporis soluta distinctio quibusdam dicta totam.",
      "created_at":"1984-11-22 17:20:39",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":6,
            "is_handled":0,
            "user_id":14,
            "content":"This comment is against the rule",
            "reportable_id":49,
            "reportable_type":"App\\Comment",
            "created_at":"2017-08-01 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":14,
               "name":"Dr. Seth Treutel",
               "email":"rigoberto.vonrueden@example.org",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ],
      "post":{  
         "id":8,
         "user_id":6,
         "title":"Quia perspiciatis aperiam quos quasi.",
         "body":"Dolore corrupti voluptatibus voluptate perferendis totam eos ut. Suscipit ea voluptas consectetur aut sed natus quia quam. Ut omnis dolore aliquid.",
         "created_at":"2013-08-12 14:19:59",
         "updated_at":"2018-11-28 20:36:19"
      }
   },
   "6":{  
      "id":42,
      "user_id":7,
      "post_id":7,
      "content":"Laborum occaecati voluptatem sed quisquam. Culpa labore voluptatibus ut dolorem. Rerum est labore dolorum unde.",
      "created_at":"1977-05-16 18:17:46",
      "updated_at":"2018-11-28 20:36:19",
      "reports":[  
         {  
            "id":8,
            "is_handled":0,
            "user_id":19,
            "content":"Not a great comment for the site",
            "reportable_id":42,
            "reportable_type":"App\\Comment",
            "created_at":"2017-08-05 20:43:08",
            "updated_at":"2018-11-28 20:36:19",
            "user":{  
               "id":19,
               "name":"Fritz Beer",
               "email":"gleason.kris@example.org",
               "email_verified_at":"2018-11-28 20:36:19",
               "created_at":"2018-11-28 20:36:19",
               "updated_at":"2018-11-28 20:36:19"
            }
         }
      ],
      "post":{  
         "id":7,
         "user_id":11,
         "title":"Ducimus et commodi blanditiis nam sed eius.",
         "body":"Optio sunt quaerat inventore voluptatem dolorum ullam. Eius optio praesentium quia eos necessitatibus commodi. Eius ut qui dolore repellendus itaque aut corrupti nobis. Libero eligendi omnis enim eveniet incidunt amet consequatur.",
         "created_at":"2005-07-28 04:59:56",
         "updated_at":"2018-11-28 20:36:19"
      }
   }
}

正如您从输出sortByDesc('created_at);中看到的那样,它不起作用,我绝对不知道如何对它进行分页。

尝试2

我还尝试查询所有报告并将这些报告分组在一起。

$reports = Report::with('reportable','user')->latest()->paginate();
return $reports->groupBy('reportable_id');

输出:

{  
   "3":[  
      {  
         "id":2,
         "is_handled":0,
         "user_id":1,
         "content":"This is a horrible post",
         "reportable_id":3,
         "reportable_type":"App\\Post",
         "created_at":"2017-08-04 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":3,
            "user_id":20,
            "title":"Dolore dicta similique sapiente consequuntur deserunt voluptas vel.",
            "body":"Provident ut aut minima iure veniam. Minus placeat temporibus sit. Quos aliquid natus rerum.",
            "created_at":"1996-01-14 01:25:34",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":1,
            "name":"Keegan Schultz",
            "email":"breanne09@example.com",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "10":[  
      {  
         "id":5,
         "is_handled":0,
         "user_id":15,
         "content":"Rule 2 broken on this post",
         "reportable_id":10,
         "reportable_type":"App\\Post",
         "created_at":"2017-08-10 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":10,
            "user_id":13,
            "title":"Eaque amet sed quia quo.",
            "body":"Rem ut illum asperiores nostrum. Aut ipsam ut qui quisquam quis iure. Itaque incidunt earum velit est est hic reprehenderit pariatur. Rerum ipsum fuga incidunt omnis repellat.",
            "created_at":"1971-07-08 05:14:57",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":15,
            "name":"Fritz Champlin",
            "email":"beth.dach@example.org",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "14":[  
      {  
         "id":4,
         "is_handled":0,
         "user_id":13,
         "content":"Rule 1 broken on this post",
         "reportable_id":14,
         "reportable_type":"App\\Post",
         "created_at":"2017-08-08 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":14,
            "user_id":16,
            "title":"Labore alias quaerat placeat et.",
            "body":"Voluptatem sit occaecati excepturi quod est voluptatem. Earum provident ut aut.",
            "created_at":"2018-05-25 02:38:31",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":13,
            "name":"Blair Conn",
            "email":"hollis.ferry@example.com",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      },
      {  
         "id":9,
         "is_handled":0,
         "user_id":10,
         "content":"Rule 1 broken on this comment",
         "reportable_id":14,
         "reportable_type":"App\\Comment",
         "created_at":"2017-08-07 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":14,
            "user_id":5,
            "post_id":13,
            "content":"Nobis possimus temporibus iure molestiae. Impedit optio nobis quae. Sunt ut voluptatem libero sunt similique sit.",
            "created_at":"1984-02-20 04:45:37",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":10,
            "name":"Neva Daugherty",
            "email":"hadley17@example.com",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      },
      {  
         "id":3,
         "is_handled":0,
         "user_id":7,
         "content":"Not a great post for the site",
         "reportable_id":14,
         "reportable_type":"App\\Post",
         "created_at":"2017-08-06 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":14,
            "user_id":16,
            "title":"Labore alias quaerat placeat et.",
            "body":"Voluptatem sit occaecati excepturi quod est voluptatem. Earum provident ut aut.",
            "created_at":"2018-05-25 02:38:31",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":7,
            "name":"Francesco Kuhic",
            "email":"amya.daniel@example.com",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      },
      {  
         "id":1,
         "is_handled":0,
         "user_id":8,
         "content":"This posts is against the rules",
         "reportable_id":14,
         "reportable_type":"App\\Post",
         "created_at":"2017-08-02 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":14,
            "user_id":16,
            "title":"Labore alias quaerat placeat et.",
            "body":"Voluptatem sit occaecati excepturi quod est voluptatem. Earum provident ut aut.",
            "created_at":"2018-05-25 02:38:31",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":8,
            "name":"Dr. Juston Hansen MD",
            "email":"priscilla.hamill@example.net",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "31":[  
      {  
         "id":7,
         "is_handled":0,
         "user_id":17,
         "content":"This is a horrible comment",
         "reportable_id":31,
         "reportable_type":"App\\Comment",
         "created_at":"2017-08-03 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":31,
            "user_id":1,
            "post_id":17,
            "content":"Dolores voluptatem eveniet animi tempora et accusantium. Aut dolores molestias reiciendis in accusamus ex. Ab hic quia non corrupti fuga.",
            "created_at":"2001-05-19 22:21:47",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":17,
            "name":"Lacey Walter",
            "email":"emard.rene@example.org",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "34":[  
      {  
         "id":10,
         "is_handled":0,
         "user_id":6,
         "content":"Rule 2 broken on this comment",
         "reportable_id":34,
         "reportable_type":"App\\Comment",
         "created_at":"2017-08-09 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":34,
            "user_id":17,
            "post_id":12,
            "content":"Deleniti dolorem esse exercitationem illum quidem. Quia inventore molestias tenetur officiis. Distinctio facilis corporis omnis molestiae. Ut corporis culpa error atque consequatur.",
            "created_at":"1994-04-11 09:26:17",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":6,
            "name":"Bernita Hahn",
            "email":"tiffany99@example.org",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "42":[  
      {  
         "id":8,
         "is_handled":0,
         "user_id":19,
         "content":"Not a great comment for the site",
         "reportable_id":42,
         "reportable_type":"App\\Comment",
         "created_at":"2017-08-05 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":42,
            "user_id":7,
            "post_id":7,
            "content":"Laborum occaecati voluptatem sed quisquam. Culpa labore voluptatibus ut dolorem. Rerum est labore dolorum unde.",
            "created_at":"1977-05-16 18:17:46",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":19,
            "name":"Fritz Beer",
            "email":"gleason.kris@example.org",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ],
   "49":[  
      {  
         "id":6,
         "is_handled":0,
         "user_id":14,
         "content":"This comment is against the rule",
         "reportable_id":49,
         "reportable_type":"App\\Comment",
         "created_at":"2017-08-01 20:43:08",
         "updated_at":"2018-11-28 20:36:19",
         "reportable":{  
            "id":49,
            "user_id":20,
            "post_id":8,
            "content":"Quis commodi a aut aut. Assumenda sit officiis placeat ipsam sit veritatis vel qui. Corporis soluta distinctio quibusdam dicta totam.",
            "created_at":"1984-11-22 17:20:39",
            "updated_at":"2018-11-28 20:36:19"
         },
         "user":{  
            "id":14,
            "name":"Dr. Seth Treutel",
            "email":"rigoberto.vonrueden@example.org",
            "email_verified_at":"2018-11-28 20:36:19",
            "created_at":"2018-11-28 20:36:19",
            "updated_at":"2018-11-28 20:36:19"
         }
      }
   ]
}

以下是显示我要获取的输出的图像:

enter image description here

是否有一种相对有效的方法? Do I have to do a raw query on the DB and use Union and join like this other thread?,以及如何对输出进行分页?有什么方法可以组织或分解报告表以更好地处理此问题?

1 个答案:

答案 0 :(得分:1)

假设您的所有关系均已正确设置并且使用反向查找,请尝试使用Constraining Eager Loads

  • 发布hasMany报告
  • 评论HasMany报告
  • 报告属于发件人
  • 报告属于评论

https://laravel.com/docs/5.7/eloquent-relationships

查看定义关系的反面

    package main

    import "fmt"

    func put(number chan<- int, count int) {
        i := 0
        for ; i <= (5 * count); i++ {
            number <- i
        }
        number <- -1
    }

    func subs(number chan<- int) {
        i := 10
        for ; i <= 19; i++ {
            number <- i
        }
    }

    func main() {
        channel1 := make(chan int)
        channel2 := make(chan int)
        done := 0
        sum := 0

        go subs(channel2)
        go put(channel1, 1)
        go put(channel1, 2)
        go put(channel1, 3)
        go put(channel1, 4)
        go put(channel1, 5)

        for done != 5 {
            select {
            case elem := <-channel1:
                if elem < 0 {
                    done++
                } else {
                    sum += elem
                    fmt.Println(sum)
                }
            case sub := <-channel2:
                sum -= sub
                fmt.Printf("atimta : %d\n", sub)
                fmt.Println(sum)
            }
        }
        close(channel1)
        close(channel2)
    }