laravel中外部定义whereIn的用法和where中使用in

laravel版本5.5

 外部定义whereIn的用法

if (!empty($id)) {
    $where[] = [
        function ($query) use ($id) {
            $query->whereIn('id', $id);
        },
    ];
}

where中使用in

如何在where中使用in,需要改底层的代码:当$operator == ‘in’ 的时候调用whereIn ,经测试是可以的(加入代码的位置,是放在if(is_array($column)))的后面。

 public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
        // If the column is an array, we will assume it is an array of key-value pairs
        // and can add them each as a where clause. We will maintain the boolean we
        // received when the method was called and pass it into the nested where.
        if (is_array($column)) {
            return $this->addArrayOfWheres($column, $boolean);
        }
 
        if($operator === 'in'){
            return $this->whereIn($column,$value,$boolean);
        }

 

 备注:位置在vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php  版本的不同laravel 可能位置不同

Laravel,where方法是查询构建器的一个重要方法,它用于添加WHERE子句到查询。除了基本的where用法外,Laravel还提供了一些高级的where用法。 以下是Laravelwhere高级使用方法的介绍: 1. Where子句的数组 可以在where子句使用一个数组,以匹配多个条件。例如,以下代码可以查找用户表名字为John且年龄为25的用户: ``` $users = DB::table('users') ->where([ ['name', '=', 'John'], ['age', '=', 25], ]) ->get(); ``` 2. Or Where语句 可以使用orWhere方法添加一个或多个where子句,以匹配多个条件的任意一个。例如,以下代码可以查找用户表名字为John或年龄为25的用户: ``` $users = DB::table('users') ->where('name', 'John') ->orWhere('age', 25) ->get(); ``` 3. Where子句的闭包 可以在where子句使用一个闭包,以添加更复杂的逻辑。例如,以下代码可以查找用户表年龄在18到30之间的用户: ``` $users = DB::table('users') ->where(function ($query) { $query->where('age', '>=', 18) ->where('age', '<=', 30); }) ->get(); ``` 4. Where子句的子查询 可以在where子句使用子查询,以匹配另一个查询结果。例如,以下代码可以查找用户表邮编在某个邮编列表的用户: ``` $zipcodes = [10001, 10002, 10003]; $users = DB::table('users') ->whereIn('zipcode', function ($query) use ($zipcodes) { $query->select('zipcode') ->from('locations') ->whereIn('zipcode', $zipcodes); }) ->get(); ``` 这些是Laravelwhere高级使用方法的一些例子,希望可以帮助你更好地了解和使用Laravel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值