【问题标题】:Laravel orwhere and whereBetween query not working togetherLaravel orwhere 和 whereBetween 查询不能一起工作
【发布时间】:2018-04-04 04:15:48
【问题描述】:

我有一种情况要从数据库中过滤用户,方法是从具有特定 id 的表的两列中过滤,并且应该使用日期范围进行过滤。

我的代码如下所示。

$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。那是有效的。但它不会一起工作。

【问题讨论】:

  • 你能解释一下你想在这里实现什么,你的查询用一些例子吗?

标签: php database laravel laravel-5.4


【解决方案1】:

使用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();

【讨论】:

  • 如何将用户 ID 传递给此
猜你喜欢
  • 2016-11-27
  • 2018-09-09
  • 1970-01-01
  • 2017-01-03
  • 2015-02-22
  • 2018-09-02
  • 1970-01-01
  • 2022-07-06
  • 2018-01-16
相关资源
最近更新 更多