laravel5.7使用技巧

综合话题

任务调度

在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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值