关于 laravel:Eloquent 查询列表项可见性约束 | 珊瑚贝

Eloquent query to list items visibility constraints


我正在为一个 Eloquent 查询而苦苦挣扎。我的最终目标是列出任何人撰写的所有公开帖子,以及给定用户在给定出版物中撰写的私人帖子。

例子:
鉴于这 3 个帖子

标题:标题 1 |用户 ID:1 | is_private: 真

标题:标题 2 |用户 ID:2 | is_private: 假

标题:标题 3 |用户 ID:3 | is_private: 真

userId 1 的输出应该是:

标题 1 和标题 2

我最好的方法是:

1
2
3
4
5
6
    $posts =  Post::WHERE(‘publication_id’, $publicationId)
        ->WHERE(‘is_private’, FALSE)
        ->WHERE(FUNCTION ($query) {
            $query->WHERE(‘owner_id’, auth()->id())
                ->WHERE(‘is_private’, TRUE);
        });

我知道这远远不是我们需要的。


如果第二个 where 语句更改为 orWhere

可能会起作用

1
2
3
4
5
6
$posts = Post::WHERE(‘publication_id’, $publicationId)
    ->WHERE(‘is_private’, FALSE)
    ->orWhere(FUNCTION ($query) {
        $query->WHERE(‘owner_id’, auth()->id)
            ->WHERE(‘is_private’, ‘true’);
    });

原因是第一个 where 将限制第二个 where 仅搜索公共帖子以检查私人帖子。这不会产生预期的结果。

一切顺利。

  • 那时我有点接近。这正是我所需要的。谢谢


你应该试试这个:

1
2
3
4
$posts =  Post::WHERE(‘publication_id’, $publicationId)
                       ->WHERE(‘is_private’, FALSE)
                       ->orWhere(‘owner_id’, auth()->id())
                       ->GET();

试试这个

1
2
3
4
5
$posts =  Post::WHERE(‘publication_id’, $publicationId)
    ->WHERE(FUNCTION ($query) {
        $query->WHERE(‘is_private’, FALSE)
            ->orWhere(‘owner_id’, auth()->id());
    });

来源:https://www.codenong.com/49449124/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8797.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?