1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
static function actions_logs($type, $options=null){ DB::enableQueryLog(); $result = DB::table('member_action_log as a') ->join('member as b', 'b.id', '=', 'a.editor') ->leftJoin('member as c', 'c.id', '=', 'a.member') ->join('options as d', 'd.id', '=', 'a.action_id') ->select('a.*', 'b.username as editor_name', 'c.username as member_name', 'd.name as action_name') ->where('b.type', '=', $type); if(val($options, 'keyword') != false){ $keyword = val($options, 'keyword'); $result = $result->where(function ($query) use ($keyword) { $query->orWhere('b.username', 'like', "%" . $keyword . "%") ->orWhere('b.name', 'like', "%" . $keyword . "%"); }); } $result = $result->orderBy('a.created_time', 'desc') ->get(); $queries = DB::getQueryLog(); logg(end($queries)); return $result; } |
Reference:
- http://wbkuo.pixnet.net/blog/post/202784386-%5Bphp%5D-laravel-5-%E5%8F%96%E5%BE%97%E6%9C%80%E5%BE%8C%E6%9F%A5%E8%A9%A2%E7%9A%84-sql-%E8%AA%9E%E6%B3%95%28last-query%29
- https://stackoverflow.com/questions/27753868/how-to-get-the-query-executed-in-laravel-5-dbgetquerylog-returning-empty-arr
Replace ? with values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public static function getQueryString() { $queryLog = DB::getQueryLog(); $lastQuery = end($queryLog); $stringSql = $lastQuery['query']; // 取代所有問號 $stringSql = preg_replace("/\?/", "'?'", $stringSql); // query 重組 foreach( $lastQuery['bindings'] as $arg ) { $stringSql = preg_replace("/\?/", $arg, $stringSql, 1); } return $stringSql; } |