There is no problem not worth solving, and no technology not worth learning!

Analysis of Protective Protectives in Laravel 6

编辑模型,在 $fillable 属性中添加 checksum。最后生成的 SQL 符合预期,字段 checksum 的值已经被插入

1. In Laravel 6, execute the UpdateOrCreate method to update the existing model or create a new model if it does not exist. The last generated sql is as follows, the field checksum is missing. as shown in Figure 1

在 Laravel 6 中,执行 updateOrCreate 方法,以更新现有模型或在不存在的情况下则创建新的模型。最后生成的 SQL 如下,缺少字段 checksum
Figure 1

insert into
  `table` (
    `asset_key`,
    `theme_id`,
    `mime_type`,
    `created_at`,
    `updated_at`,
    `content`,
    `schema`,
    `category`
  )
values
  (
    'apps/internal/back-top/app.json',
    '989925b6-58d3-4fee-a50f-8cd084462bf6',
    'application/json',
    '2023-03-03 09:21:47',
    '2023-03-03 09:21:47',
    '{\n  \"title\": \"回到顶部\",\n  \"description\": \"为页面增加返回到页面顶部的按钮,提高用户体验。\"\n}\n',
    null,
    'unknown'
  )


2. The code is implemented as follows:


$predicate = [
	'asset_key' => $to.$view['asset_key'],
	'theme_id' => $themeId,
];

$extra = [
	'mime_type' => $view['mime_type'],
	'created_at' => $view['modified_at'],
	'updated_at' => $view['modified_at'],
	'content' => $content,
	'schema' => $schema,
	'checksum' => $checksum,
	'category' => $category
];

ThemeAsset::updateOrCreate(
	$predicate,
	$extra
);


3. Print the variable $extra, and determine that the field checksum is not null, which means that the field checksum is omitted in the sql statement because its value is null. as shown in Figure 2

打印变量 $extra,确定其中的字段 checksum 不为 null,说明并不是因为其值为 null 则在 SQL 语句中省略了字段 checksum
Figure 2

array(7) {
  ["mime_type"]=>
  string(16) "application/json"
  ["created_at"]=>
  object(Illuminate\Support\Carbon)#6487 (19) {
    ["endOfTime":protected]=>
    bool(false)
    ["startOfTime":protected]=>
    bool(false)
    ["constructedObjectId":protected]=>
    string(32) "000000004ec81aa8000000003e76cedb"
    ["localMonthsOverflow":protected]=>
    NULL
    ["localYearsOverflow":protected]=>
    NULL
    ["localStrictModeEnabled":protected]=>
    NULL
    ["localHumanDiffOptions":protected]=>
    NULL
    ["localToStringFormat":protected]=>
    NULL
    ["localSerializer":protected]=>
    NULL
    ["localMacros":protected]=>
    NULL
    ["localGenericMacros":protected]=>
    NULL
    ["localFormatFunction":protected]=>
    NULL
    ["localTranslator":protected]=>
    NULL
    ["dumpProperties":protected]=>
    array(3) {
      [0]=>
      string(4) "date"
      [1]=>
      string(13) "timezone_type"
      [2]=>
      string(8) "timezone"
    }
    ["dumpLocale":protected]=>
    NULL
    ["dumpDateProperties":protected]=>
    NULL
    ["date"]=>
    string(26) "2023-03-03 09:53:51.598327"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  ["updated_at"]=>
  object(Illuminate\Support\Carbon)#6487 (19) {
    ["endOfTime":protected]=>
    bool(false)
    ["startOfTime":protected]=>
    bool(false)
    ["constructedObjectId":protected]=>
    string(32) "000000004ec81aa8000000003e76cedb"
    ["localMonthsOverflow":protected]=>
    NULL
    ["localYearsOverflow":protected]=>
    NULL
    ["localStrictModeEnabled":protected]=>
    NULL
    ["localHumanDiffOptions":protected]=>
    NULL
    ["localToStringFormat":protected]=>
    NULL
    ["localSerializer":protected]=>
    NULL
    ["localMacros":protected]=>
    NULL
    ["localGenericMacros":protected]=>
    NULL
    ["localFormatFunction":protected]=>
    NULL
    ["localTranslator":protected]=>
    NULL
    ["dumpProperties":protected]=>
    array(3) {
      [0]=>
      string(4) "date"
      [1]=>
      string(13) "timezone_type"
      [2]=>
      string(8) "timezone"
    }
    ["dumpLocale":protected]=>
    NULL
    ["dumpDateProperties":protected]=>
    NULL
    ["date"]=>
    string(26) "2023-03-03 09:53:51.598327"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(3) "UTC"
  }
  ["content"]=>
  string(120) "{
  "title": "回到顶部",
  "description": "为页面增加返回到页面顶部的按钮,提高用户体验。"
}
"
  ["schema"]=>
  NULL
  ["checksum"]=>
  string(40) "2a0fe2215b02b5738926ff93cde566d43ad8188e"
  ["category"]=>
  string(7) "unknown"
}


4. Edit the model and add checksum to the $fillable property. The last generated SQL is as expected, and the value of the field checksum has been inserted. as shown in Figure 3

编辑模型,在 $fillable 属性中添加 checksum。最后生成的 SQL 符合预期,字段 checksum 的值已经被插入
Figure 3

    protected $fillable = [
        'asset_key',
        'theme_id',
        'content',
        'mime_type',
        'created_at',
        'updated_at',
        'category',
        'checksum',
        'schema'
    ];



insert into
  `table` (
    `asset_key`,
    `theme_id`,
    `mime_type`,
    `created_at`,
    `updated_at`,
    `content`,
    `schema`,
    `checksum`,
    `category`
  )
values
  (
    'apps/internal/back-top/app.json',
    '989931f5-7e35-4943-b11d-00a0b900626c',
    'application/json',
    '2023-03-03 09:56:07',
    '2023-03-03 09:56:07',
    '{\n  \"title\": \"回到顶部\",\n  \"description\": \"为页面增加返回到页面顶部的按钮,提高用户体验。\"\n}\n',
    null,
    '2a0fe2215b02b5738926ff93cde566d43ad8188e',
    'unknown'
  )


PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.