Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

laravel other #23

Open
clms2 opened this issue Oct 15, 2018 · 0 comments
Open

laravel other #23

clms2 opened this issue Oct 15, 2018 · 0 comments

Comments

@clms2
Copy link
Owner

clms2 commented Oct 15, 2018

  • 使用DB::unprepared()可以执行lock tables之类的命令
  • 如果是跨库关联,比如pgsql模型1对1mysql模型,那么需要两个模型都指定protected $connection,否则引号会判断错
  • 可使用$request->offsetSet('key', 'value')在中间件里修改/新增参数, offsetSet设置的参数可直接通过$request->all()获取到,如果通过$request->aa = 'value'设置的话不会在->all()里获取到
  • 可通过$model->newCollection([$model1, $model2])把原本是很多模型的数组变成可以使用$models->load()预加载关系的形式
  • 在列表页包含筛选条件的时候,如果用模型进行筛选,然后还要group by某个字段,那么普通的$model->groupBy('field')->count()获取到的总数是第一个分组的数量是错的,解决方案是在获取总数时使用$model->count(DB::raw('distinct field'))获取,这样原本带筛选条件的模型, clone一下就可以用来统计总数了
  • linux队列简单监听执行:nohup php artisan queue:work --queue=email --timeout=180 --tries=3 --delay=1 > /dev/null 2>&1 &
  • api如果用的cookie认证,且用了encryptCookies中间件,那么在api加载的中间件顺序时候需要把encryptCookies放第一个
  • 使用集合的merge函数时,如果是普通的集合(Illuminate\Support\Collection)那么是正常覆盖相同键的;如果集合是模型(从数据库查询出来的Eloquent对象),那么merge时会使用主键,而不是指定的key,所以keyBy无效, 源码在\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Collection.phpmerge函数,可以看到使用了模型的getKey()函数获取主键。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant