laravel框架中超实用的功能介绍

本篇文章给大家带来的内容是关于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 AppProviders;

 

use IlluminateSupportServiceProvider;

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()

    {

        //

    }

}

链接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取码:x2p5

免费分享,但是X度限制严重,如若链接失效点击链接或搜索加群 群号518475424

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框架中超实用的功能介绍的详细内容

原文地址:https://www.cnblogs.com/it-3327/p/11794959.html