简体   繁体   English

Laravel orwhere和whereBetween查询无法一起使用

[英]Laravel orwhere and whereBetween query not working together

I have a situation to filter user from database in a way that to filter from two columns of the table with a particular id and that should be filtered with a range of date. 我有一种情况可以从数据库中过滤用户,这种方式可以从具有特定ID的表的两列中进行过滤,并且应该以日期范围进行过滤。

My code is as shown below. 我的代码如下所示。

$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();

what I have tried is put a static date, and is not working. 我尝试过的是放置一个静态日期,并且不起作用。 Also I removed orWhere and whereBetween independently. 我也单独删除了orWherewhereBetween That is working. 可以了 But it will not working together. 但是它不会一起工作。

Use where() closure to group your conditional query: 使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM