首页 > 其他分享 >Hyperf 插入json数据需要注意

Hyperf 插入json数据需要注意

时间:2024-07-15 13:18:49浏览次数:11  
标签:instance Hyperf 插入 json book attributes integer array id

我本来的代码是使用了firstOrCreate ,但是实际create才会调用到

模型文件

protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'shop_id' => 'integer', 'question_id' => 'integer', 'book_category_id' => 'integer', 'user_id' => 'integer', 'book_id' => 'integer','images'=>'array'];

使用地方

UserQuestionFeedback::create([
    'book_id' => $book_id,
    'book_category_id' => $bookInfo['categories_id'],
    'user_id' => $user['id'],
    'question_id' => $question_id,
    'phone' => $phone,
    'content' => $content,
    'images' => $images,
    'shop_id' => $this->shop_id
]);

 

create跟firstOrcreate源码

public function create(array $attributes = [])
    {
        return tap($this->newModelInstance($attributes), function ($instance) {
            $instance->save();
        });
    }

public function firstOrCreate(array $attributes, array $values = [])
    {
        if (! is_null($instance = $this->where($attributes)->first())) {
            return $instance;
        }

        return tap($this->newModelInstance($attributes + $values), function ($instance) {
            $instance->save();
        });
    }

这个问题就很明显了

标签:instance,Hyperf,插入,json,book,attributes,integer,array,id
From: https://www.cnblogs.com/sdfgdrg/p/18302962

相关文章

  • 使用JSONObject构建与解析json对象(简易版)
    构建json实例化一个JSONObject对象,而后调用其put()方法,将数据写入。put()方法的第一个参数为key值,必须为String类型,第二个参数为value,可以为boolean、double、int、long、Object、Map以及Collection等。当然,double以及int等类型只是在Java中,写入到json中时,统一都会以Number类......
  • 重复的第二行前插入一行
    问题:在重复序列数的第二行前插入一行Countif+查找法:使用以下公式返回每个值出现的次数=COUNTIF(A$3:A3,A3)选取B列》查找》查找内容:2》查找范围:值》查找全部Ctrl+A选取所有查找结果后关闭查找对话框在单元格上方插入一行纯公式法:=LET(x,A3:A13,y,SORT(VSTACK(......
  • 希尔排序--插入排序升级版
    #include<stdio.h>//希尔排序函数voidshellSort(intarr[],intsize){inti,j,gap,temp;//计算初始增量gap=1;while(gap<size){gap=gap*3+1;//Knuth增量序列for(i=gap;i<size;i++){//当前待插......
  • IO输入输出流例子:Java对象输出json文本:
    读取文件:原始字节输入流(低级):publicclassCharCacheIOReader{publicstaticvoidmain(String[]args){try(//原始字节输入流(低级)Readerfr=newFileReader("src\\OutputStream.txt");//创建一个字......
  • package.json 脚本配置使用环境文件
    脚本使用环境文件"scripts":{"dev":"vite","build:prod":"vitebuild","build:stage":"vitebuild--modestaging","preview":"vitepreview"}dev:运行开发服务器,默认使用.......
  • 解决HBuilder X运行微信小程序模拟器Error: pages.json解析失败
    前言如果已经排查很久了,那这就不是你的问题了,大概率是由于你曾经创建了一个路径,在指定PagePath的时候又指向了这个路径,这个操作本身没有问题。但是,如果你曾经对这个路径修改过了,那编译器就会有问题,来品鉴一下这个错误。16:15:36.772请注意运行模式下,因日志输出、sourcem......
  • C# Newtonsoft.Json 高级用法
    一、基本用法Json.Net是支持序列化和反序列化DataTable,DataSet,EntityFramework和Entity的。下面分别举例说明序列化和反序列化。//序列化DataTableDataTabledt=newDataTable();dt.Columns.Add("Age",Type.GetType("System.Int32"));dt.Columns.Add("Name",Type.Get......
  • 新手教学系列——高效管理MongoDB数据:批量插入与更新的实战技巧
    前言在日常开发中,MongoDB作为一种灵活高效的NoSQL数据库,深受开发者喜爱。然而,如何高效地进行数据的批量插入和更新,却常常让人头疼。今天,我们将一起探讨如何使用MongoDB的bulk_write方法,简化我们的数据管理流程,让代码更加简洁高效。常规做法:find、insertone、updateone在处......
  • 主流json解析框架示例
    主流json解析框架示例jackson、gson、fastjson/fastjson2三种主流json解析框架对比●性能:在性能方面,Fastjson通常被认为是最快的JSON解析库,其次是Jackson和Gson,json-lib的性能相对较低。●API和功能:Jackson提供了非常灵活、强大的API,支持各种高级功能,例如树模型、数据绑定、......
  • linux-Rsyslog自定义配置json模板
    配置日志接收模板和转发参考:https://www.cnblogs.com/xwupiaomiao/p/17565418.html自定义模板配置文件在主配置文件中添加(rsyslog.conf)include(file=“/etc/rsyslog.d/*.conf”mode=“optional”)方案一在/etc/rsyslog.d/下创建一个配置文件ct3a1.conf#日志模板......