Home > PHP Framework > Laravel > Introduction to super practical functions in laravel framework

Introduction to super practical functions in laravel framework

不言
Release: 2018-10-15 14:37:09
forward
2756 people have browsed it

This article brings you an introduction to the super practical functions in the laravel framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Make lumen's dd() dump() as elegant as laravel

composer require symfony/var-dumper
Copy after login

Get the executed sql statement

Can view sql where parameters, etc.

    public function index()
    {
        DB::connection()->enableQueryLog(); // 开启查询日志
        
        DB::table('posts')->paginate(5);  //要查看的sql

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

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

Can only view simple sql but cannot see the incoming parameters

DB::table('posts')->toSql();
Copy after login

Query sql records

If, you want to save the log files in the storage/logs directory. Needs to be updated: The boot() function in app/Providers/AppServiceProvider.php

<?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()
    {
        //
    }
}
Copy after login

Laravel How to get the value of a field before modification in the model event

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");
    }
});
Copy after login

The above is the detailed content of Introduction to super practical functions in laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template