Laravel orwhere 和 whereBetween 查询不能一起工作

Laravel orwhere and whereBetween query not working together

我有一种情况可以从数据库中过滤用户,这种方式可以从具有特定 ID 的 table 的两列中进行过滤,并且应该使用日期范围进行过滤。

我的代码如下图

$fetchSelectedUser=Wallet_Transaction::select('wallet__transactions.from as FromUser','wallet__transactions.type','wallet__transactions.date','wallet__transactions.amount','wallet__transactions.balance_after',
               'wallet__transactions.type','wallet__transactions.description','wallet__transactions.to as ToUser','users.name as FromName',DB::raw('(select name from users where users.id = ToUser) as toName'))
               ->join('users','users.id','=','wallet__transactions.from')
               ->where('wallet__transactions.from','=',$user_id)->orWhere('wallet__transactions.to','=',$user_id)
               ->whereBetween('wallet__transactions.db_date',[$fromDate,$toDate])->get();

我试过的是设置静态日期,但没有用。我还单独删除了 orWherewhereBetween。那是有效的。但它不会一起工作。

使用 where() 闭包对您的 conditional 查询进行分组:

$fetchSelectedUser=Wallet_Transaction::select('wallet__transactions.from as FromUser','wallet__transactions.type','wallet__transactions.date','wallet__transactions.amount','wallet__transactions.balance_after',
           'wallet__transactions.type','wallet__transactions.description','wallet__transactions.to as ToUser','users.name as FromName',DB::raw('(select name from users where users.id = ToUser) as toName'))
           ->join('users','users.id','=','wallet__transactions.from')
           ->where(function($q) use ($user_id){
                $q->where('wallet__transactions.from','=',$user_id)->orWhere('wallet__transactions.to','=',$user_id);
           })->whereBetween('wallet__transactions.db_date',[$fromDate,$toDate])->get();