小编Gop*_*tam的帖子

如何在 laravel eloquent 查询生成器中实现“where not”

这个 mysql sql 片段的 laravel 等效雄辩查询生成器是什么:

select * from `posts` left join `user_post_interactions` on `posts`.`id` = `user_post_interactions`.`post_id` where `posts`.`user_id` = 10 and not (`user_post_interactions`.`interaction_name` = 'hide' and `user_post_interactions`.`user_id` = 10)
Run Code Online (Sandbox Code Playgroud)

我正在做的是:

$this->posts()->leftJoin('user_post_interactions', 'posts.id', '=', 'user_post_interactions.post_id')
            ->where(function($q) {
                $q->where('user_post_interactions.interaction_name', '<>', 'hide')
                    ->where('user_post_interactions.user_id', '<>', 10);
            });
Run Code Online (Sandbox Code Playgroud)

但这并没有产生我预期的结果

sql laravel eloquent laravel-5

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

eloquent ×1

laravel ×1

laravel-5 ×1

sql ×1