侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

在Laravel中预先搜索JSON列类型并使用WHEN方法

Laravel  /  管理员 发布于 1年前   510

作为常规程序员,高级搜索是您不时遇到的事情。 我最近在Laravel中实现了它。 我要在这里解释。

先决条件-假设您确实具有Laravel Architecture及其功能(例如模型,控制器和路由的工作方式)的基础知识。

好的,我们确实有一个生物数据表,因为名称代表它保存了用户的生物数据。 这就是它的迁移方式


Migration

$table->id();
$table->enum('gender',['boy','girl']);
$table->string('name',255);
$table->json('biodata');
$table->timestamps();

如您所见,我们确实有一个表列生物数据,它是JSON类型。 这使我们可以将JSON保存在数据库中。

 并且我们将使用属性强制转换在获取时将其自动转换为对象。 所以我们的生物数据模型看起来像这样


Model

namespace App;
use Illuminate\Database\Eloquent\Model;
class Biodata extends Model
{
    protected $guarded = [];
    protected $casts = [
       'biodata' => object
    ];
    protected $perPage = 10;
}

现在,在进一步介绍之前,先看看我们的生物数据对象是什么样子


Factory

return [
            'gender' => $this->faker->randomElement($this->gender),
            'name'=> $this->faker->name(),
            'biodata' => [
                'personal'=>[
                    'qualification' => $this->faker->randomElement($this->qualification),
                    'height'=>$this->faker->randomElement($this->height),
                    'income'=>$this->faker->numberBetween(100000,1000000),
                    'occupation' => $this->faker->randomElement($this->jobs),
                ],
                'family'=>[
                    'father_gotra' => $this->faker->randomElement($this->gotra),
                    'mother_gotra' => $this->faker->randomElement($this->gotra),
                ],
                'contact'=>[
                    'state' => $this->faker->randomElement($this->indianStates),
                ]
            ],
        ];

在所有这些字段上,我们将进行搜索。现在该讨论我们想从搜索功能中获得什么。 所以这是我们所有可选的搜索参数

Gender:单值  ,  字段类型-ENUM
Qualification:可能是多个值,  字段类型-JSON密钥
Min Height:单值  ,  字段类型-JSON密钥
Income:单值  ,  字段类型-JSON密钥
Occupation:单值  ,  字段类型-JSON密钥
Gotra:多重价值  ,  字段类型-JSON密钥

现在,这对您来说似乎很简单,您只需要在where语句中使用,就可以了,但是请紧紧抓住,然后让我们先解决一些明显的问题


在JSON字段中搜索?

那么我们如何在JSON中搜索呢? 很显然,这对于MYSQL来说并不难,对于Laravel来说甚至更好,简而言之,这是您在JSON字段中进行搜索的方式,前提是它的密钥

return $query->where('biodata->personal->income', '>=', $minIncome);

检查上面的生物数据对象以了解。


多个值

现在,在进入编写搜索功能的代码之前,您可能要问的一个问题是如何在查询字符串中发送多个值? 很高兴您提出要求,我们只会将它们添加这样的逗号

http:://url.com/?qualification=MBA,BA&gender=boy

现在,我们可以简单地使用“,”将其爆炸,这将为我们提供该参数的值数组。


如何将多个查询添加到where语句?

所以这是Laravel文档声明的标准

DB::table('users')->where('votes', 100)->get();

或者 最好这么写

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

您可以在此处向where语句添加多个参数,

但是,在实际跳至运行任何查询之前,我们需要进行某种验证。由于所有参数都是可选的,因此用户可能不会提供要添加到查询中的全部或任何输入。

因此,当我们在寻找地点和地点时,我们需要编写额外的代码来拯救我们。 这就是官方文件所说的

有时,您可能希望子句仅在其他情况成立时才应用于查询。 例如,如果传入请求中存在给定的输入值,则您可能只想应用where语句。 您可以使用when方法完成此操作:

这就是你写的方式

$users = DB::table('users')
                ->when($role, function ($query, $role) {
                    return $query->where('role_id', $role);
                })
                ->get();

但是我们还有一个问题,我们如何为同一个参数添加多个值,那么when是函数的第二个参数,我们可以在该函数中执行任何操作:)继续阅读

因此,为了检查同一参数的多个值,我们将在其中使用循环,这就是它的样子。

->when($gotra, function ($query, $gotra) {
               foreach($gotra as $g){
                   $query->where('biodata->family->father_gotra', '<>' ,$g);
               }
               return $query;
           })

就是这样了。 我们介绍了编写搜索功能所需的所有基础知识。 编写的demo:

public function search(){
       $gender = request('gender');
       $state = request('state');
       $minIncome = (int)request('minIncome');
       $minHeight = (int)request('minHeight');
       $qualifications = request('qualification') != '' ? explode(",", request('qualification') ) : false;
       $gotra = request('gotra') != '' ? explode(",", request('gotra') ) : false;
       $results = Biodata::
           when($gender, function ($query, $gender) {
               return $query->where('gender', $gender);
           })
           ->when($state, function ($query, $state) {
               return $query->where('biodata->contact->state', $state);
           })
           ->when($qualifications, function ($query, $minQualification) {
               foreach($qualifications as $qualification){
                   $query->where('biodata->personal->qualification', '=', $qualification);
               }
               return $query;
           })
           ->when($minIncome, function ($query, $minIncome) {
               return $query->where('biodata->personal->income', '>=', $minIncome);
           })
           ->when($minHeight, function ($query, $minHeight) {
               return $query->where('biodata->personal->height', '>=' , $minHeight);
           })
           ->when($gotra, function ($query, $gotra) {
               foreach($gotra as $g){
                   $query->where('biodata->family->father_gotra', '<>' ,$g);
               }
               return $query;
           })
           ->paginate(10);
          return response($results,200);

在开发此程序时,我遵循了TDD方法,因此我首先创建了测试,让我知道是否有人要检出它们。 给我发送消息,我将分享代码。

因此,今天我将把这个问题留给你们,让我知道您的想法。


转:https://dev.to/jiteshdhamaniya/advance-searching-in-laravel-for-json-column-type-and-when-method-3dp2



  • 上一条:
    PHP编写英文及数字验证码
    下一条:
    建议,想法,思路,来水!_友链交换留言!
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 9.24版本发布(0个评论)
    • Laravel collect集合中获取二维数组中键值功能示例代码(0个评论)
    • lumen中验证类的实现及使用流程步骤(0个评论)
    • 构建你自己的Laravel扩展包的流程步骤(0个评论)
    • Laravel 9.23版本发布(0个评论)
    • 近期文章
    • GnuPG(GPG)生成用于替代SSH密钥的子密钥:签名、加密、鉴权及SSH验证(0个评论)
    • GnuPG(GPG)密钥创建的流程步骤(0个评论)
    • Laravel 9.24版本发布(0个评论)
    • windows系统phpstudy环境中安装amqp拓展流程步骤(0个评论)
    • windows10+docker desktop使用docker compose编排多容器构建dnmp环境(0个评论)
    • windows10+docker desktop运行laravel项目报错:could not find driver...(0个评论)
    • windows10+docker desktop报错:docker: Error response from daemon: user declined directory sharing(0个评论)
    • go语言中Pat多路复用器路由功能示例代码(0个评论)
    • go语言中HttpRouter多路复用器路由功能示例代码(0个评论)
    • js中使用Push.js通知库将通知推送到浏览器(0个评论)
    • 近期评论
    • nkt 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 用了三分钟就被禁了,直接阿里云服务器22端口都禁了..
    • 熊丽 在

      安装docker + locust + boomer压测环境实现对接口的压测中评论 试试水..
    • 博主 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 @test  也可能是国内大环境所至,也是好事,督促你该研究学习新技术..
    • test 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 打了一次网页,然后再也打不开了。。是阿里云的缘故吗?..
    • 博主 在

      centos7中Meili Search搜索引擎安装流程步骤中评论 @鹿   执行以下命令看看你的2.27版本是否存在strin..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客