首頁 > php框架 > Laravel > laravel框架超實用的功能介紹

laravel框架超實用的功能介紹

不言
發布: 2018-10-15 14:37:09
轉載
2800 人瀏覽過

這篇文章帶給大家的內容是關於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);  //要查看的sql

 

    $queries = DB::getQueryLog(); // 获取查询日志

 

    dd($queries); // 即可查看执行的sql,执行的时间,传入的参数等等

}

登入後複製

只能查看簡單的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

{

    /**

     * Bootstrap any application services.

     *

     * @return void

     */

    public function boot()

    {

        //

        // 新增代码

        DB::listen(function ($query) {

            Log::info(

                $query->sql,

                $query->bindings,

                $query->time

            );

        });

    }

 

    /**

     * Register any application services.

     *

     * @return void

     */

    public function register()

    {

        //

    }

}

登入後複製

Laravel 如何在模型事件中取得某欄位修改前的值

1

2

3

4

5

6

7

8

Issue::saving(function(Issue $issue){

    if ($issue->isDirty(&#39;title&#39;)) {

        $user = Auth::user()->username;

        $oldTitle = $issue->getOriginal(&#39;title&#39;); // 原始值

        $newTitle = $issue->title;                // 新值

        ActionLog::log("$user 把标题 $oldTitle 修改为 $newTitle");

    }

});

登入後複製

以上是laravel框架超實用的功能介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板