如何在 laravel eloquent 上添加括号? (Laravel 5.3)
How can I add brackets on the laravel eloquent? (Laravel 5.3)
我的查询是这样的:
SELECT * FROM message WHERE (seller_id = 1 OR buyer_id = 3) AND deleted_by NOT LIKE '%1%'
我想改成laraveleloquent
我这样试:
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 eloquent 上添加括号?
试试这个:
Message::where(function ($query) {
$query->where('seller_id', auth()->user()->id)
->orWhere('buyer_id', auth()->user()->id);
})->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')->get();
您需要将 where 放在闭包中
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();
Message::where(function ($query) {
$query->where('seller_id', auth()->user()->id)
->orWhere('buyer_id', auth()->user()->id);
})
->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')
->get();
试试这个,也许你的问题就解决了。
在这种情况下,使用闭包。
$userId = auth()->user()->id;
Message::where(function($q) use($userId){
$q->where('buyer_id', $userId)
->orWhere('buyer_id', $userId);
})
->where('deleted_by', 'not like', '%'.$userId.'%')
->get();
我的查询是这样的:
SELECT * FROM message WHERE (seller_id = 1 OR buyer_id = 3) AND deleted_by NOT LIKE '%1%'
我想改成laraveleloquent
我这样试:
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 eloquent 上添加括号?
试试这个:
Message::where(function ($query) {
$query->where('seller_id', auth()->user()->id)
->orWhere('buyer_id', auth()->user()->id);
})->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')->get();
您需要将 where 放在闭包中
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();
Message::where(function ($query) {
$query->where('seller_id', auth()->user()->id)
->orWhere('buyer_id', auth()->user()->id);
})
->where('deleted_by', 'not like', '%'.auth()->user()->id.'%')
->get();
试试这个,也许你的问题就解决了。
在这种情况下,使用闭包。
$userId = auth()->user()->id;
Message::where(function($q) use($userId){
$q->where('buyer_id', $userId)
->orWhere('buyer_id', $userId);
})
->where('deleted_by', 'not like', '%'.$userId.'%')
->get();