PHP 对象动态传参数(反射) / Laravel Cast 的相关使用用例 作者: Chuwen 时间: 2021-10-20 分类: Laravel,PHP 评论 ## 序言 主要在用到 Laravel Cast,所以有了这个想法,暂时记录下用法,后面再慢慢解释(如果有空 哈哈哈哈) ## 代码 `MailSendLogComment.php` ```php $this->test ]; } } ``` `MailSendLogCommentCast.php`: ```php newInstanceArgs(json_decode($value, true)); } return $value; } /** * Prepare the given value for storage. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value * @param array $attributes * * @return array */ public function set($model, $key, $value, array $attributes): array { if ($key === 'comments') { // 如果给的值不是 MailSendLogComment 对象就报错 if ($value instanceof MailSendLogComment) { throw new InvalidArgumentException('The given value is not an ' . MailSendLogComment::class . ' instance.'); } $attributes['comments'] = json_encode($value->toArray(), JSON_UNESCAPED_UNICODE); } return $attributes; } } ``` `MailSendLog.php` ```php MailSendLogCommentCast::class, 'type' => MailSendLogTypeEnum::class// 限定必须使用此枚举类型 ]; } ``` ## 用法 ```php // 查询数据 use App\Models\MailSendLog; dump(MailSendLog::first()->comments);// 返回的是 \App\Casts\Manage\MailSendLogComment 对象 dump(MailSendLog::first()->comments->test);// 返回的是 \App\Casts\Manage\MailSendLogComment 对象 test 属性的值 // 新增数据 $M = new MailSendLog(); $M->title = '邮件标题'; $M->content = '邮件内容'; $M->comments = new MailSendLogComment('邮件备注'); //必须传入这个类型,否则将会报错 $M->exception = '错误日志'; $M->save();// 保存 ```
2021 王者荣耀 6 周年皮肤返场投票排行榜单查看 作者: Chuwen 时间: 2021-10-16 分类: 唠嗑闲聊 评论 ## 网址: https://nowtime.cc/pvp/ 
Vue.js 组件 script 分离 作者: Chuwen 时间: 2021-10-14 分类: Vue.js 评论 ## 需要 通常情况下,在单 `.vue` 文件开发很方便,但是当业务逻辑起来了,那么经常需要修改 `script` 和 `template` 部分,用编辑器分屏也不是那么好操作,故有了**组件 script 分离** ## 例子 ### `TableList.vue` ```vue 测试 {{test}} ``` ### `index.js` > 在 `TableList.vue` 同级创建个如 `index.js` 文件 ```js export default { name: "TableList", components: { 'pagination': Pagination }, data(){ return { test: 'test' } } } ```
注册账号时,密码表单使浏览器提示建议的密码(现代主流浏览器 Chrome、Safari、Firefox 支持) 作者: Chuwen 时间: 2021-10-12 分类: HTML5 评论 ## 序言 常看到一些网站注册账号时,在密码表单浏览器会提示“建议安全系数高的密码”   Google 上查阅了很多相关的资料,最终找到了相关用法: - https://www.chromium.org/developers/design-documents/form-styles-that-chromium-understands - https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/autocomplete - https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-autocomplete 主要靠的是 `autocomplete` 属性来实现  ## 代码 > 请注意,必须使用 `http`/`https` 协议预览才有效果 ```html ```
解决 Spug 发布前任务出错: Exception: 'utf-8' codec can't decode bytes in position 116-117: invalid continuation byte 作者: Chuwen 时间: 2021-09-12 分类: Linux 评论 ## 环境 简单描述下我的环境,在 macOS 上装的 Docker,在 Docker 里面安装了 Spug。 然后在 Spug 添加主机,用 ssh 连接 本机,模拟相关环境构建、发布 - Docker for macOS: `Docker version 20.10.8, build 3967b7d` - Spug API版本: `v3.0.1-beta.9` - Spug Web版本: `v3.0.1-beta.9` ## 发布前任务出错 Exception: 'utf-8' codec can't decode bytes in position 116-117: invalid continuation byte ## 究其原因 直到我用 Web Console 连接本机 ssh,执行了下 `ll` 发现有的文件夹名字是 `????` 一看就发现问题了,原来是中文乱码了,我就想着报出上述的错误可能就是这个原因了。 ## 解决办法 假设你使用的是 `oh-my-zsh`,执行以下命令: ```shell vim ~/.zshrc ``` 然后在文件末尾添加上这里两句话: ``` export LC_ALL=zh_CN.UTF-8 export LANG=zh_CN.UTF-8 ``` 然后保存并退出,先按 `ESC` 然后英文输入状态下输入 `:wq`,再按 `Enter` 即可 然后使配置文件生效: ```shell source ~/.zshrc ```