Laravel illuminate/database
组件的SQL日志打印
- 基于
illuminate/database
5.0
在低版本的组件里,有一个事件监听字符 illuminate.query
,
public function listen(Closure $callback)
{
if (isset($this->events))
{
$this->events->listen('illuminate.query', $callback);
}
}
传入一个闭包,然后就可以打印所有的SQL语句,实例:
<?php
/**
* Notes:
* File name:DataBaseServe
* Create by: Jay.Li
* Created on: 2019/11/11 0011 9:38
*/
namespace App\Serve;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
class DataBaseServe
{
public $databaseServe = null;
public function __construct()
{
$this->databaseServe = new Capsule();
$this->initDatabase();
}
public function initDatabase()
{
$this->databaseServe->addConnection(require __DIR__ . '/../../config/database.php');
$this->databaseServe->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$this->databaseServe->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$this->databaseServe->bootEloquent