這篇文章帶給大家的內容是關於laravel框架中超實用的功能介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
讓lumen的dd() dump()像laravel一樣優雅
1 | composer require symfony/ var -dumper
|
登入後複製
取得執行的sql語句
#可查看sql where參數等
1 2 3 4 5 6 7 8 9 10 | public function index()
{
DB::connection()->enableQueryLog();
DB::table('posts')->paginate(5);
$queries = DB::getQueryLog();
dd( $queries );
}
|
登入後複製
只能查看簡單的sql不能看到傳入的參數
1 | DB::table('posts')->toSql();
|
登入後複製
查詢sql記錄
如果,你想要將日誌檔案保存在storage/logs 目錄中。需要更新: app/Providers/AppServiceProvider.php 裡的 boot() 函數
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 27 28 29 30 31 32 33 34 35 36 37 38 | <?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use DB;
use Log;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
DB::listen( function ( $query ) {
Log::info(
$query ->sql,
$query ->bindings,
$query ->time
);
});
}
public function register()
{
}
}
|
登入後複製
Laravel 如何在模型事件中取得某欄位修改前的值
1 2 3 4 5 6 7 8 | Issue::saving( function (Issue $issue ){
if ( $issue ->isDirty('title')) {
$user = Auth::user()->username;
$oldTitle = $issue ->getOriginal('title');
$newTitle = $issue ->title;
ActionLog::log( "$user 把标题 $oldTitle 修改为 $newTitle" );
}
});
|
登入後複製
以上是laravel框架超實用的功能介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!