kanagama / laravel-eloquent-method-expansion
Eloquent 方法扩展
Requires
- php: ^7.4 | ^8.0 | ^8.1 | ^8.2
- laravel/framework: ^8.0 | ^9.0 | ^10.0
Requires (Dev)
- orchestra/testbench: *
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0 | ^10.0
- dev-master
- v1.3.0
- v1.2.4.x-dev
- v1.2.4
- v1.2.3
- v1.2.2.x-dev
- v1.2.2
- v1.2.1.x-dev
- v1.2.1
- v1.2.0.x-dev
- v1.2.0
- v1.1.1.x-dev
- v1.1.1
- v1.1.0.x-dev
- v1.1.0
- v1.0.3.x-dev
- v1.0.3
- v1.0.2.x-dev
- v1.0.2
- v1.0.1.x-dev
- v1.0.1
- v1.0.0
- v0.9.2.x-dev
- v0.9.2
- v0.9.1.x-dev
- v0.9.1
- v0.9.0
- v0.2.0.x-dev
- v0.2.0
- v0.1.0.x-dev
- v0.1.0
- v0.0.3.x-dev
- v0.0.3
- v0.0.2.x-dev
- v0.0.2
- v0.0.1.x-dev
- v0.0.1
- dev-delete_dockerfile_version
- dev-codacy
This package is auto-updated.
Last update: 2024-09-09 00:10:58 UTC
README
功能概述
php7.4 以上 Laravel8.0 以上
扩展 Eloquent,添加方法。
(※实际上扩展的是QueryBuilder,但是因为已经决定了仓库名称,所以…)
packagist
https://packagist.org.cn/packages/kanagama/laravel-eloquent-method-expansion
用法
使用 composer 安装
composer require kanagama/laravel-eloquent-method-expansion
安装后,以下方法可以使用。
扩展 where 子句
※ [columnName] 应输入表列名,使用大写字母。
where[columnName]IsNull(), orWhere[columnName]IsNull()
where[columnName]IsNull() 方法是添加 columnName 为 NULL 的条件。
$users = DB::table('users') ->whereNameIsNull() ->get(); # select * from users where name IS NULL;
where[columnName]IsNotNull(), orWhere[columnName]IsNotNull()
where[columnName]IsNotNull() 方法是添加 columnName 不为 NULL 的条件。
$users = DB::table('users') ->whereNameIsNotNull() ->get(); # select * from users where name IS NOT NULL;
where[columnName]Eq(), orWhere[columnName]Eq()
※ 兼容AllowEmpty 选项
where[columnName]Eq() 方法是添加参数值与 columnName 值一致的条件。
$users = DB::table('users') ->whereTelEq('09099999999') ->get(); # select * from users where tel = '09099999999';
where[columnName]NotEq(), orWhere[columnName]NotEq()
※ 兼容AllowEmpty 选项
where[columnName]NotEq() 方法是添加参数值与 columnName 值不一致的条件。
$users = DB::table('users') ->whereTelNotEq('09099999999') ->get(); # select * from users where tel <> '09099999999';
where[columnName]Gt(), orWhere[columnName]Gt()
※ 兼容AllowEmpty 选项
where[columnName]Gt() 方法是添加参数值大于 columnName 的条件。
$users = DB::table('users') ->whereCreatedAtGt('1980-05-21') ->get(); # select * from users where created_at > '1980-05-21';
where[columnName]Gte(), orWhere[columnName]Gte()
※ 兼容AllowEmpty 选项
where[columnName]Gte() 方法是添加参数值大于等于 columnName 的条件。
$users = DB::table('users') ->whereCreatedAtGte('1980-05-21') ->get(); # select * from users where created_at >= '1980-05-21';
where[columnName]Lt(), orWhere[columnName]Lt()
※ 兼容AllowEmpty 选项
where[columnName]Lt() 方法是添加参数值小于 columnName 的条件。
$users = DB::table('users') ->whereModifiedAtLt('1980-05-21 00:00:00') ->get(); # select * from users where modified_at < '1980-05-21 00:00:00';
where[columnName]Lte(), orWhere[columnName]Lte()
※ 兼容AllowEmpty 选项
where[columnName]Lte() 方法是添加参数值小于等于 columnName 的条件。
$users = DB::table('users') ->whereModifiedAtLte('1980-05-21 00:00:00') ->get(); # select * from users where modified_at <= '1980-05-21 00:00:00';
where[columnName]In(), orWhere[columnName]In()
※ 兼容AllowEmpty 选项
where[columnName]In() 方法是添加 columnName 的值包含在指定数组中的条件。
$users = DB::table('users') ->whereUserStatusIdIn([ '1','2','3', ]) ->get(); # select * from users where user_status_id in (1, 2, 3);
where[columnName]NotIn(), orWhere[columnName]NotIn()
※ 兼容AllowEmpty 选项
where[columnName]NotIn() 方法是添加 columnName 的值不包含在指定数组中的条件。
$users = DB::table('users') ->whereUserStatusIdNotIn([ '1','2','3', ]) ->get(); # select * from users where user_status_id not in (1, 2, 3);
where[columnName]Like(), orWhere[columnName]Like()
※ 兼容AllowEmpty 选项
where[columnName]Like 方法是添加 columName 的值中包含参数值的条件。
$users = DB::table('users') ->whereAddressLike('沖縄県') ->get(); # select * from users where address like '%沖縄県%';
where[columnName]NotLike(), orWhere[columnName]NotLike()
※ 兼容AllowEmpty 选项
where[columnName]NotLike 方法是添加 columName 的值中不包含参数值的条件。
$users = DB::table('users') ->whereAddressNotLike('沖縄県') ->get(); # select * from users where address not like '%沖縄県%';
where[columnName]LikePrefix(), orWhere[columnName]LikePrefix()
※ 兼容AllowEmpty 选项
where[columnName]LikePrefix 方法是添加 columName 的值中前方包含参数值的条件。
$users = DB::table('users') ->whereAddressLikePrefix('沖縄県') ->get(); # select * from users where address like '沖縄県%';
where[columnName]NotLikePrefix(), orWhere[columnName]NotLikePrefix()
※ 兼容AllowEmpty 选项
where[columnName]LikePrefix 方法是添加 columName 的值中不包含参数值的前方匹配条件。
$users = DB::table('users') ->whereAddressNotLikePrefix('沖縄県') ->get(); # select * from users where address not like '沖縄県%';
where[columnName]LikeBackword(), orWhere[columnName]Backword()
※ 兼容AllowEmpty 选项
where[columnName]LikeBackword 方法是添加 columName 的值中包含参数值的条件。
$users = DB::table('users') ->whereAddressLikeBackword('沖縄県') ->get(); # select * from users where address like '%沖縄県';
where[columnName]NotLikeBackword(), orWhere[columnName]NotBackword()
※ 兼容AllowEmpty 选项
where[columnName]LikeBackword 方法是添加 columName 的值中不包含参数值的后方匹配条件。
$users = DB::table('users') ->whereAddressNotLikeBackword('沖縄県') ->get(); # select * from users where address not like '%沖縄県';
where[columnName]Date(), orWhere[columnName]Date()
※ 兼容AllowEmpty 选项
where[columnName]Date 方法是添加 columName 的值与日期比较的条件。
$users = DB::table('users') ->whereRentDatetimeDate('2022-12-02') ->get(); # select * from `products` where date(`rent_datetime`) = "2022-12-02"
where[columnName]DateGt(), orWhere[columnName]DateGt()
※ 兼容AllowEmpty 选项
where[columnName]DateGt 方法是添加 columName 的值与日期使用 > 比较的条件。
$users = DB::table('users') ->whereRentDatetimeDateGt('2022-12-02') ->get(); # select * from `products` where date(`rent_datetime`) > "2022-12-12"
where[columnName]DateGte(), orWhere[columnName]DateGte()
※ 兼容AllowEmpty 选项
where[columnName]DateGte 方法是添加 columName 的值与日期使用 >= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeDateGte('2022-12-02') ->get(); # select * from `products` where date(`rent_datetime`) >= "2022-12-12"
where[columnName]DateLt(), orWhere[columnName]DateLt()
※ 兼容AllowEmpty 选项
where[columnName]DateLt 方法是添加 columName 的值与日期使用 < 比较的条件。
$users = DB::table('users') ->whereRentDatetimeDateLt('2022-12-02') ->get(); # select * from `products` where date(`rent_datetime`) < "2022-12-12"
where[columnName]DateLte(), orWhere[columnName]DateLte()
※ 兼容AllowEmpty 选项
where[columnName]DateLte 方法是添加 columName 的值与日期使用 <= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeDateLte('2022-12-02') ->get(); # select * from `products` where date(`rent_datetime`) <= "2022-12-12"
where[columnName]Month(), orWhere[columnName]Month()
※ 兼容AllowEmpty 选项
where[columnName]Month 方法是添加 columName 的值与特定月份比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonth('12') ->get(); # select * from `products` where month(`rent_datetime`) = "12"
where[columnName]MonthGt(), orWhere[columnName]MonthGt()
※ 兼容AllowEmpty 选项
where[columnName]MonthGt 方法是添加 columName 的值与特定月份使用 > 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthGt('10') ->get(); # select * from `products` where month(`rent_datetime`) > "10"
where[columnName]MonthGte(), orWhere[columnName]MonthGte()
※ 兼容AllowEmpty 选项
where[columnName]MonthGte 方法是添加 columName 的值与特定月份使用 >= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthGte('10') ->get(); # select * from `products` where month(`rent_datetime`) >= "10"
where[columnName]MonthLt(), orWhere[columnName]MonthLt()
※ 兼容AllowEmpty 选项
where[columnName]MonthLt 方法是添加 columName 的值与特定月份使用 < 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthLt('10') ->get(); # select * from `products` where month(`rent_datetime`) < "10"
where[columnName]MonthLte(), orWhere[columnName]MonthLte()
※ 兼容AllowEmpty 选项
where[columnName]MonthLte 方法是添加 columName 的值与特定月份使用 <= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthLte('10') ->get(); # select * from `products` where month(`rent_datetime`) <= "10"
where[columnName]Day(), orWhere[columnName]Day()
※ 兼容AllowEmpty 选项
where[columnName]Day 方法是添加 columName 的值与特定日期比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonth('31') ->get(); # select * from `products` where day(`rent_datetime`) = "31"
where[columnName]DayGt(), orWhere[columnName]DayGt()
※ 兼容AllowEmpty 选项
where[columnName]DayGt 方法是添加 columName 的值与特定日期使用 > 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthGt('15') ->get(); # select * from `products` where day(`rent_datetime`) > "15"
where[columnName]DayGte(), orWhere[columnName]DayGte()
※ 兼容AllowEmpty 选项
where[columnName]DayGte 方法是添加 columName 的值与特定日期使用 >= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthGte('15') ->get(); # select * from `products` where day(`rent_datetime`) >= "15"
where[columnName]DayLt(), orWhere[columnName]DayLt()
※ 兼容AllowEmpty 选项
where[columnName]DayLt 方法是添加 columName 的值与特定日期使用 < 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthLt('15') ->get(); # select * from `products` where day(`rent_datetime`) < "15"
where[columnName]DayLte(), orWhere[columnName]DayLte()
※ 兼容AllowEmpty 选项
where[columnName]DayLte 方法是添加 columName 的值与特定日期使用 <= 比较的条件。
$users = DB::table('users') ->whereRentDatetimeMonthLte('15') ->get(); # select * from `products` where day(`rent_datetime`) <= "15"
where[columnName]Year(), orWhere[columnName]Year()
※ 兼容AllowEmpty 选项
where[columnName]Year 方法是添加 columName 的值与特定年份比较的条件。
$users = DB::table('users') ->whereRentDatetimeYear('2022') ->get(); # select * from `products` where year(`rent_datetime`) = "2022"
where[columnName]YearGt(), orWhere[columnName]YearGt()
※ 兼容AllowEmpty 选项
where[columnName]YearGt() 方法是使用 > 运算符比较 columnName 的值与特定年份。
$users = DB::table('users') ->whereRentDatetimeYearGt('2022') ->get(); # select * from `products` where year(`rent_datetime`) > "2022"
where[columnName]YearGte(), orWhere[columnName]YearGte()
※ 兼容AllowEmpty 选项
where[columnName]YearGte() 方法是使用 >= 运算符比较 columnName 的值与特定年份。
$users = DB::table('users') ->whereRentDatetimeYearGte('2022') ->get(); # select * from `products` where year(`rent_datetime`) >= "2022"
where[columnName]YearLt(), orWhere[columnName]YearLt()
※ 兼容AllowEmpty 选项
where[columnName]YearLt() 方法是使用 < 运算符比较 columnName 的值与特定年份。
$users = DB::table('users') ->whereRentDatetimeYearLt('2022') ->get(); # select * from `products` where year(`rent_datetime`) < "2022"
where[columnName]YearLte(), orWhere[columnName]YearLte()
※ 兼容AllowEmpty 选项
where[columnName]YearLte() 方法是使用 <= 运算符比较 columnName 的值与特定年份。
$users = DB::table('users') ->whereRentDatetimeYearLte('2022') ->get(); # select * from `products` where year(`rent_datetime`) <= "2022"
where[columnName]Time(), orWhere[columnName]Time()
※ 兼容AllowEmpty 选项
where[columnName]Time() 方法是使用比较 columnName 的值与特定时间。
$users = DB::table('users') ->whereRentDatetimeTime('12:00:00') ->get(); # select * from `products` where time(`rent_datetime`) = "12:00:00"
where[columnName]TimeGt(), orWhere[columnName]TimeGt()
※ 兼容AllowEmpty 选项
where[columnName]TimeGt() 方法是使用 > 运算符比较 columnName 的值与特定时间。
$users = DB::table('users') ->whereRentDatetimeTimeGt('12:00:00') ->get(); # select * from `products` where time(`rent_datetime`) > "12:00:00"
where[columnName]TimeGte(), orWhere[columnName]TimeGte()
※ 兼容AllowEmpty 选项
where[columnName]TimeGte() 方法是使用 >= 运算符比较 columnName 的值与特定时间。
$users = DB::table('users') ->whereRentDatetimeTimeGte('12:00:00') ->get(); # select * from `products` where time(`rent_datetime`) >= "12:00:00"
where[columnName]TimeLt(), orWhere[columnName]TimeLt()
※ 兼容AllowEmpty 选项
where[columnName]TimeLt() 方法是使用 < 运算符比较 columnName 的值与特定时间。
$users = DB::table('users') ->whereRentDatetimeTimeLt('12:00:00') ->get(); # select * from `products` where time(`rent_datetime`) < "12:00:00"
where[columnName]TimeLte(), orWhere[columnName]TimeLte()
※ 兼容AllowEmpty 选项
where[columnName]TimeLte() 方法是使用 <= 运算符比较 columnName 的值与特定时间。
$users = DB::table('users') ->whereRentDatetimeTimeLte('12:00:00') ->get(); # select * from `products` where time(`rent_datetime`) <= "12:00:00"
where[columnName]Column(), orWhere[columnName]Column()
※ 兼容AllowEmpty 选项
where[columnName]Column() 方法是添加一个条件,使得 columnName 与指定的列的值相等。
$users = DB::table('users') ->whereRentDateColumn('return_date') ->get(); # select * from `products` where `rent_date` = `return_date`
where[columnName]ColumnGt(), orWhere[columnName]ColumnGt()
※ 兼容AllowEmpty 选项
where[columnName]ColumnGt() 方法是添加一个条件,使得 columnName 比指定的列的值大。
$users = DB::table('users') ->whereRentDateColumnGt('return_date') ->get(); # select * from `products` where `rent_date` > `return_date`
where[columnName]ColumnGte(), orWhere[columnName]ColumnGte()
※ 兼容AllowEmpty 选项
where[columnName]ColumnGt() 方法是添加一个条件,使得 columnName 至少与指定的列的值一样大。
$users = DB::table('users') ->whereRentDateColumnGte('return_date') ->get(); # select * from `products` where `rent_date` >= `return_date`
where[columnName]ColumnLt(), orWhere[columnName]ColumnLt()
※ 兼容AllowEmpty 选项
where[columnName]ColumnLt() 方法是添加一个条件,使得 columnName 比指定的列的值小。
$users = DB::table('users') ->whereRentDateColumnLt('return_date') ->get(); # select * from `products` where `rent_date` < `return_date`
where[columnName]ColumnLte(), orWhere[columnName]ColumnLte()
※ 兼容AllowEmpty 选项
where[columnName]ColumnLte() 方法是添加一个条件,使得 columnName 至多与指定的列的值一样小。
$users = DB::table('users') ->whereRentDateColumnLt('return_date') ->get(); # select * from `products` where `rent_date` <= `return_date`
where[columnName]Between(), orWhere[columnName]Between()
※ 兼容AllowEmpty 选项
where[columnName]Between() 方法是添加一个条件,使得 columnName 的值在两个值之间。
$users = DB::table('users') ->whereCreatedAtBetween(['2022-12-01', '2022-12-10',]) ->get(); # select * from users where created_at between '2022-12-01' AND '2022-12-10'
where[columnName]NotBetween(), orWhere[columnName]NotBetween()
※ 兼容AllowEmpty 选项
where[columnName]NotBetween() 方法是添加一个条件,使得 columnName 的值不在两个值之间。
$users = DB::table('users') ->whereCreatedAtNotBetween(['2022-12-01', '2022-12-10',]) ->get(); # select * from users where created_at not between '2022-12-01' AND '2022-12-10'
allowEmpty
allowEmpty 选项
在 where 后添加 AllowEmpty 选项,当参数为 null、[] 或字符串 '' 时,将省略该条件。
# $rentDatetime = null; # $returnDatetime = '1980-05-21 00:00:00' $users = DB::table('users') ->whereAllowEmptyRentDatetimeGte($rentDatetime) ->whereAllowEmptyReturnDatetimeGte($returnDatetime) ->get(); # null のため、whereAllowEmptyRentDatetimeGte() は省略される # select * from users where return_datetime >= '1980-05-21 00:00:00';
以下为 AllowEmpty 检查的条件
# 渡されたパラメータがこの条件を満たさない場合、その条件は省略されます # int の 0 と string の '0" は省略させたくなかったので、この条件にしています if (!empty($parameters) || is_numeric($parameter)) {
allowEmpty 的用法
在管理界面(例如预订管理界面)中,可能已准备了预订者姓名、预订日期、电子邮件地址、电话号码、预订状态等众多筛选条件,您需要检查这些条件是否为空,不为空则添加到筛选条件中。使用 Eloquent 来编写这一过程可能需要相当多的工作量。
$query = DB::table('reservations'); # 絞り込み条件がリクエストパラメータに存在しているかチェックして where に追加しないといけない if ($request->reserve_name) { $query->where('reservations.name', 'LIKE', '%'. $request->reserve_name . '%'); } if ($request->reserve_date) { $query->whereDate('reservations.reserve_date_at', $request->reserve_date); } if ($request->email) { $query->whereEmail($request->email); } if ($request->tel) { $query->whereTel($request->tel); } if ($request->status) { $query->whereStatus($request->status); } // 絞り込み条件の数だけ続く
不要用全部 if 语句包围,这时可以使用 AllowEmpty 选项。
当传递 null 或 [] 时,将省略筛选条件,因此可以方便地进行方法链调用。
return DB::table('reservations') ->whereAllowEmptyNameLike($request->reserve_name) ->whereAllowEmptyReserveDateAtDate($request->reserve_date) ->whereAllowEmptyEmailEq($request->email) ->whereAllowEmptyTelEq($request->tel) ->whereAllowEmptyStatusEq($request->status) ->get();
AllowEmpty 选项不可用
- where[columnName]IsNull()
- orWhere[columnName]IsNull()
- where[columnName]Null()
- orWhere[columnName]Null()
- where[columnName]NotNull()
- orWhere[columnName]NotNull()
- where[columnName]IsNotNull()
- orWhere[columnName]IsNotNull()
扩展 orderBy 子句
orderBy[columnName]Asc()
orderBy[columnName]Asc() 方法是按 columnName 的升序排序。
$users = DB::table('users') ->orderByCreatedAtAsc() ->get(); # select * from users order by created_at asc
orderBy[columName]Desc()
orderBy[columnName]Desc() 方法是按 columnName 的降序排序。
$users = DB::table('users') ->orderByCreatedAtDesc() ->get(); # select * from users order by created_at desc
orderBy[columnName]Field()
orderBy[columnName]Field() 方法是按 columnName 指定的顺序排序。 为了将 null 放在末尾,将附加 Desc
$users = DB::table('users') ->orderByIdField([2, 1, 4, 3,]) ->get(); # select * from users order by FIELD(id, 3, 4, 1, 2) DESC;
开发容器
make deelopment-build make development
PHPUnit 容器
PHP7.4 PHP8.0 PHP8.1 PHP8.2
make test-build
make test
※启动容器时执行测试