Laravel Mix 判断是否为生产环境 作者: Chuwen 时间: 2021-12-22 分类: Laravel 评论 有这么个需求,需要在一些静态文件带上 hash 参数,解决 CDN 缓存问题,但是在网上找的用这个 `mix.config.production` 是会报错的 然后自己打印 `mix` 发现了一个 `inProduction()` 方法,验证了下,这个确实是判断是否为生产环境的 #### 修改项目根目录下 webpack.mix.js ```js const mix = require('laravel-mix'); mix.js('resources/js/app.js', 'public/js') .postCss('resources/css/app.css', 'public/css', [ require("tailwindcss"), ]); if (mix.inProduction()) mix.version(); ``` #### 然后看 /public/mix-manifest.json 会发现内容是这样的 ```json { "/js/app.js": "/js/app.js?id=33663d66c33666962f26", "/css/app.css": "/css/app.css?id=7066f8a66603866e666" } ``` #### 只要你保证在你的 blade 模版里面使用如 `mix('js/app.js')` 这样引用的就没有问题 ```blade @yield('title') @yield('content') ``` #### 最终生成的代码是类似 `` 这样的
Laravl 在 Mailer 类里获取渲染后的模版内容 作者: Chuwen 时间: 2021-10-21 分类: Laravel,PHP 评论 ## 序言 因为在项目中需要保存下发送的邮件内容,以便后续查看 ## 代码 通过以下代码就会获取到: ```php return Container::getInstance()->make('mailer')->render( $this->buildView(), $this->buildViewData() ); ``` 完整代码实例: ```php queue = 'account-send-email'; } /** * Build the message. * * @return $this */ public function build(): static { // 存放你的逻辑代码 return $this->view('emails.account.insufficient_quota_reminder'); } /** * 处理任务成功 * * @param \Illuminate\Contracts\Mail\Factory|\Illuminate\Contracts\Mail\Mailer $mailer */ public function send($mailer) { parent::send($mailer); $model = MailSendLog::whereId($this->mailSendLogId)->first(); if (is_null($model)) return; try { $model->content = $this->getRender(); } catch (BindingResolutionException | ReflectionException) { } $model->status = 1;//发送成功 $model->save(); } /** * 处理任务失败。 * * @param \Throwable $exception * * @return void */ public function failed(Throwable $exception) { $model = MailSendLog::whereId($this->mailSendLogId)->first(); if (is_null($model)) return; try { $model->content = $this->render(); } catch (ReflectionException) { } $model->exception = $exception->getMessage(); $model->status = 2;//发送失败 $model->save(); } /** * 获取渲染后的邮件模版 * * @return string * @throws \Illuminate\Contracts\Container\BindingResolutionException * @throws \ReflectionException */ private function getRender(): string { return Container::getInstance()->make('mailer')->render( $this->buildView(), $this->buildViewData() ); } } ```
Laravel 模型使用观察者(Observer) 作者: Chuwen 时间: 2021-10-20 分类: Laravel,PHP 评论 参考:https://segmentfault.com/a/1190000012292973
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();// 保存 ```
做个 Larvel Model 插入或更新小笔记,因为犯错比较多的 作者: Chuwen 时间: 2021-09-03 分类: Laravel,PHP 1 条评论 ### Laravel 模型 新建数据 或者 更新数据 > 可能我这个方法不够优雅,欢迎批评指正 ```php first(); // 如果为空 if (is_null($content)) { // 创建模型实例 $content = new Content; $content->uid = $uid; } $content->text = '测试测试'; // 新建数据 或者 更新数据 $content->save(); } } ``` 再留个中文文档的相关链接:https://learnku.com/docs/laravel/8.x/eloquent/9406#c7d398