综合话题
任务调度
在linux中编辑定时任务代码,每分钟执行一次
#打开定时任务文件
ubuntu:crontab -e
#添加下列代码
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
在app/Console/Kernel.php@schedule中添加代码,每周一早上6点执行任务
$schedule->call(function(){
(new MigrateDbController())->migrate();
})->weekly()->fridays()->at('6:00');
辅助函数
array_get()
https://learnku.com/docs/laravel/5.7/helpers/2282#method-array-get
$mantisCustomFields = [7000 => [40 => 'Hign']]
$level = array_get($mantisCustomFields, "7000.40", 'NA');//结果 Hign
array_flatten($array, $depth ) 多维数组的平铺,可以指定平铺深度,默认平铺为1位数组
https://learnku.com/docs/laravel/5.7/helpers/2282#method-array-flatten
$array = [
[[1],[2],[3]],
[[4],[5],[6]]
];
$array = array_flatten($array);//[1,2,3,4,5,6];
$array = array_flatten($array, 1);//[ [1],[2],[3],[4],[5],[6] ];
邮件发送
参考:laravel 5.7 邮件发送 或者 laravelacademy 邮件发送功能实现
自定义配置文件
1、在config/目录下新建一个配置文件
2、在项目中读取配置文件
use Illuminate\Support\Facades\Config;
class {
public __construct(){
Config::get('配置文件名'.'配置文件内键名'.'配置文件二级键名');
}
}
钉钉机器人
#安装插件
ubuntu:/yourRootDirectory$ composer require calchen/laravel-dingtalk-robot-notification:^2.0
#发布后,在config下会创建dingtalk_robot.php
ubuntu:/yourRootDirectory$ php artisan vendor:publish --provider="Calchen\LaravelDingtalkRobot\DingtalkRobotNoticeServiceProvider"
未完待续...
数据库操作
DB库
获取delete/update/insert的sql语句
获取select的语句很简单,直接DB::connection('your db name')->table('table name')->where(['k'=>'v'])->toSql();即可,那么增删改呢?
如果打印结果为空,查看所有(记住是所有)的enableQueryLog()和getQueryLog()前面是否加了connection;来源:How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array
DB::connection('my_connection')->enableQueryLog();
//your delete/insert/update code
dd(DB::connection('my_connection')->getQueryLog());
查询数据库最新插入数据
$id = DB::connnection('YourDbName')->getPdo()->lastInsertId();
$id = DB::table('posts')->insertGetId($data);
https://laracasts.com/discuss/channels/general-discussion/db-insertgetid