手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

belongsToMany的备份

 纯备份,长时间不用的时候 直接就忘了

 
PHP代码
  1. class Shop{      
  2.     /** 
  3.      * 员工 
  4.      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany 
  5.      */  
  6.     public function staffs()  
  7.     {  
  8.         // 1、目标类 UnionUser,
  9. // 2、中间表的表名 pivot: union_user_store,
  10. // 3、中间表对应当前表的字段 :shopcode ,
  11. // 4、中间表对应目标类的字段 usercode  
  12.         //  当关联不是ID时,需要有5、6参数  
  13.         // 5、当前类对应 3的字段 (code -> 3:shopcode)  
  14. // 6、目标类对应4的字段 (usercode -> 4:usercode )  
  15.         return $this->belongsToMany(UnionUser::class, UnionUserStore::getTable(), 'shopcode''usercode''code', UnionUser::USERCODE);  
  16.     }  
  17. }  
 
---EOF--

laravel debug模式下抛出异常时隐藏关键字段

laravel在debug模式下抛出异常时,会把当前的环境变量等打印出来,即使是开发机、测试机,其实有时候也不希望将一些关键的变量公开给所有人,比如密码、APPKEY(主要是不少人在上传的时候APPKEY本地和服务器一致),还有各类OSS、微信的appId,secret,token等,所以就需要在抛出异常时隐藏掉。

 
网上查到的是在config/app.php中增加:debug_hide 的数组下标,对应的数据是_ENV,_SERVER,其实就是把遍历这些全局变量,对指定的KEY进行隐藏。但偶尔也查到有debug_blacklist。于是全文搜索了一下,发现框架里是:foreach([config('app.debug_hide'),config('app.debug_blacklist')]),看来这是一个历史 遗留问题。
 
知道怎么写之后就方便了。例如:
'debug_hide'=>[
    '_ENV' => ['APP_KEY']
]
,随便抛一个异常可以看到 ,输出的APP_KEY的地方,已经不再是标准值了,而是一堆***,关键变量其实有很多,如果一个个写,感觉有点累,再加上,它不支持 * 的模拟判断,比如*KEY*,则完全无效。于是偷了个懒:
'debug_hide'=>[
    '_ENV' => array_filter(array_keys($_ENV),fn($v)=> Str::contains(strtolower($v), ['oss_', 'db_', 'cookie', 'key','secret', 'password', 'aliyun']) ),
]
既然都在debug_mode下了。也不在乎这点性能了。。
tips:
1、不能用str($v)->contains,因为str这个helper函数,居然还调用了env的class,而在刚加载config文件时。env的类还没有加载进来。
2、strtolower,是因为Str::contains不能忽略大小写。虽然.env都是标准的大写,但你不能保证其他人有没有写小写(又不能抽他们)。所以先tolower一下
 
解决!
 

Sablog-x 1.6升级支持PHP8成功

 发现从PHP7.4升到PHP8后,就一个地方,那就是 get_magic_quotes_gpc 这个函数。报错信息是:PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function get_magic_quotes_gpc()。

方法未定义?看了下文档,原来从7.4.1开始就已经不再支持了,而且永远返回False,在PHP8中彻底移除,仅此而已,于是乎。。。。
在 include/common.php 文件中,在调用 get_magic_quotes_gpc上面加了一段 :
if(!function_exists('get_magic_quotes_gpc')){
    function get_magic_quotes_gpc(){
        return false;
    }
}
然后,一切正常,所以我就不再上传新包了。(页面详情可能有不正常的情况,是因为我在上一页下一页的时候。$article[articleid]上没有加单引号,进入后台在模板管理 default/show.php 搜到 57行,下一篇的链接就是 ,调整一下即可)
 
----
改完后才发现,问题很多啊。。。比如 global.php的296行,$article[image] ,这种代码太多了。。。。估计得一点点改了,php4的后遗症啊。
 
----
只能说解决了95%的问题,其它的只能遇到后慢慢改了。。。我也不可能一个个的去定义:define('image','image');。当然 这样就彻底解决了,只是有点2
 
 

Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."

 在使用Ziggy的时候,偶尔会报类似的错误:

Error in render: "Error: Ziggy error: 'project' parameter is required for route 'projectPage'."。
仔细看出错的路由,往往都是有必要参数的,比如Route::any('/test/{user:uuid}',[UserController::class,'test'])->name('test')
象这种路由,如果在JS中直接获取route().current()来获取当前路径并显示,那肯定就会报错。因为current()方法是将参数和路由一并显示出来。
route(route().current())这样的报错怎么处理呢?
其实,只要加上params参数就行了,例如:route(route().current(),route().params).toString(),为什么会有这么妖的写法?是因为在公众号的网页里,如果你要用jssdk,就必须是当前URL,如果不正确,获取的签名也就可能是失败的,因此,才想到用这个恶心的方法。当然你也可以用location.href...
 
 

aliyun composer 镜像可能有问题

 好几次遇到阿里云的镜像更新有问题了,明明某个库有了最新版,但阿里云就是报版本不存在。今天又出现了这样的情况,安装laravel-zero的时候,require-dev中有个laravel/pint ^1.2,直接就报了

 
  Problem 1
    - Root composer.json requires laravel/pint ^1.2, found laravel/pint[dev-main, v0.1.0, ..., v0.2.4, v1.0.0, ..., v1.1.3] but it does not match the constraint.
 
问题是这个laravel/pint 的github上,1.2的release是 9月13日啊。(laravel/pint: Laravel Pint is an opinionated PHP code style fixer for minimalists. (github.com))。
 
由于之前也遇到过这个问题,当时是unset掉 aliyun镜像,换成了官方镜像,但确实是慢,所以我就看了一眼其他镜像,比如以前常用的https://packagist.phpcomposer.com,在国内没有其他镜像的时候,它做了很大的贡献,然而也太不稳定了。直接报:

Problem 1 - Root composer.json requires laravel-zero/framework, it could not be found in any version, there may be a typo in the package name. Problem 2 - Root composer.json requires nunomaduro/termwind, it could not be found in any version, there may be a typo in the package name. Problem 3 - Root composer.json requires laravel/pint, it could not be found in any version, there may be a typo in the package name. Problem 4 - Root composer.json requires mockery/mockery, it could not be found in any version, there may be a typo in the package name. Problem 5 - Root composer.json requires pestphp/pest, it could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details. - It's a private package and you forgot to add a custom repository to find it Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

看来,已经多年没有更新过了,真可惜了。
最后在这里找到了:国内 PHP Composer 镜像列表 - 腾讯云开发者社区-腾讯云 (tencent.com),列表中的那些镜像,象最后的安畅网络的,网址都崩了。所幸,腾讯的还能用。
打开:腾讯软件源 (tencent.com),搜到composer,看一下帮助(真简单就两句话),一句教你配置,一句教你下载。
配好之后,composer u,完成。记录一下
 
 
1、阿里:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
2、腾讯:composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
3、清除:composer config -g --unset repos.packagist
 
带 -g 的是全局的,不带  -g,就是当前项目了。
 

 
Records:64312345678910»