编程语言
首页 > 编程语言> > php – 如何在laravel雄辩中添加括号? (Laravel 5.3)

php – 如何在laravel雄辩中添加括号? (Laravel 5.3)

作者:互联网

我的查询是这样的:

SELECT * FROM message WHERE (seller_id = 1 OR buyer_id = 3) AND deleted_by NOT LIKE '%1%'

我想把它改成laravel雄辩

我试着这样:

Message::where('seller_id', auth()->user()->id)
       ->orWhere('buyer_id', auth()->user()->id)
       ->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')
       ->get();

但是,结果是这样的:

SELECT * FROM message WHERE seller_id = 1 OR buyer_id = 3 AND deleted_by NOT LIKE '%1%'

似乎需要括号

如何在laravel雄辩中添加括号?

解决方法:

你需要把你的位置放在一个封闭处

Message::where(function($query){
    $query->where('buyer_id', auth()->user()->id)
    ->orWhere('buyer_id', auth()->user()->id);
})
->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')
->get();

标签:php,mysql,laravel,laravel-5-3,laravel-eloquent
来源: https://codeday.me/bug/20190713/1449026.html