当前位置:首页 > php > 正文内容

thinkphp6 搜索功能实现

zhangsir3年前 (2023-01-09)php151

一,thinkphp6搜索功能实现

1创建模型(例如User模型)

模型类函数的命名规范:searchFieldNameAttr,FieldName根据自己的需要随意命名。例如下面的searchNameAttr。

<?php
namespace app\model;

use think\Model;

class User extends Model 
{
    public function searchNameAttr($query, $value, $data)
    {
            //name字段的模糊查询
        $query->where('name','like', '%' . $value . '%');
    }
    
    public function searchCreateTimeAttr($query, $value, $data)
    {
            //添加时间字段的限制条件
        $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }    
}

2控制器

User::withSearch(['name','create_time'], [ 'name' => 'think',     'create_time' => ['2018-8-1','2018-8-5'],        'status' => 1    ]) ->select();


User::withSearch(['name','create_time'], [
        'name'=>'think',
        'create_time'=>['2018-8-1','2018-8-5'],
        'status'=>1
    ])
->select();

注解:User::withSearch是使用thinkphp6的搜索器触发方法,['name','create_time']是上面User模型类的searchNameAttr和searchCreateTimeAttr函数,['name'=>'think','create_time' =>['2018-8-1','2018-8-5'],status'=>1]是参数。


官方搜索器文档

zhangsir版权y1防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/94.html

标签: phpthinkphp
分享给朋友:

“thinkphp6 搜索功能实现” 的相关文章

php时间戳转换成时间

date("Y-m-d H:i:s",time())这就是PHP时间戳转成时间的方式了...

PHP数组怎么去重

1.使用array_unique方法进行去重对数组元素进行去重,我们一般会使用array_unique方法,使用这个方法可以把数组中的元素去重。<?php $arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9); $arr&nbs...

迅睿cms 通用分页样式代码分享,复制粘帖即可

{module catid=$catid  order=updatetime page=1 join=1_news_category_data on=id}       &nbs...

php 实现返回上一页

php实现返回上一页的功能的3种有效方法header(location:你的上一页的路径);   //   注意这个函数前不能有输出     header(location:.getenv(&quo...

think PHP返回上一页的办法!

think PHP返回上一页的办法!输入如下代码即可返回上一页return redirect($_SERVER["HTTP_REFERER"]);...

PHP获取当前请求的所有请求头信息

apache_request_headers()函数里面保函了所有的请求头信息//获取请求头 $headers = apache_request_headers(); var_dump($header)这样就打印出所有的请求头信息了。...