没有不值得去解决的问题,也没有不值得去学习的技术!

Analyzing Guarded Attributes in Laravel 6

Edit the model to add checksum to the $fillable property. The resulting SQL meets expectations, and the value of the checksum field has been inserted

1. In Laravel 6, execute the updateOrCreate method to update an existing model or create a new one if it does not exist. The resulting SQL is as follows, missing the checksum field. See Figure 1.

In Laravel 6, execute the updateOrCreate method to update an existing model or create a new one if it does not exist. The resulting SQL is as follows, missing the checksum field
Figure 1
SQL

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 implementation is as follows:

PHP

$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 to confirm that the checksum field is not null, which indicates that the checksum field was not omitted from the SQL statement because its value was null. See Figure 2.

Print the variable $extra to confirm that the checksum field is not null, which indicates that the checksum field was not omitted from the SQL statement because its value was null
Figure 2
Plaintext
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 to add checksum to the $fillable property. The resulting SQL meets expectations, and the value of the checksum field has been inserted. See Figure 3.

Edit the model to add checksum to the $fillable property. The resulting SQL meets expectations, and the value of the checksum field has been inserted
Figure 3
PHP

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

SQL

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'
  )

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系