Php Laravel或WHERE和WHERETHEVER查询不一起工作
我有一种从数据库中筛选用户的方式,即从具有特定id的表的两列中筛选,并且应该使用日期范围进行筛选 我的代码如下所示Php Laravel或WHERE和WHERETHEVER查询不一起工作,php,database,laravel,laravel-5.4,Php,Database,Laravel,Laravel 5.4,我有一种从数据库中筛选用户的方式,即从具有特定id的表的两列中筛选,并且应该使用日期范围进行筛选 我的代码如下所示 $fetchSelectedUser=Wallet_Transaction::select('wallet__transactions.from as FromUser','wallet__transactions.type','wallet__transactions.date','wallet__transactions.amount','wallet__transaction
$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();
我试过的是把一个静态的日期,并没有工作。我还独立地删除了或where
和wherewhere
。这是有效的。但是它不能一起工作。使用where()
闭包对条件查询进行分组:
$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();
你能解释一下你想在这里实现什么,你的查询有一些例子吗?如何将userid传递给这个