• lararvel学习文档


    学习文档

    http://laravel.p2hp.com/

    http://laravel.p2hp.com/cndocs/9.x/eloquent#generating-model-classes

    laravel9.x

    https://learnku.com/docs/laravel/9.x

    laravel8.5

    https://learnku.com/docs/laravel/8.5

    laravel8.x

    https://learnku.com/docs/laravel/8.x

    laravel7.x

    https://learnku.com/docs/laravel/7.x

    laravel6.x

    https://learnku.com/docs/laravel/6.x

    Laravel 速查表 9.x ,8.x,7.x

    https://learnku.com/docs/laravel-cheatsheet/9.x

    PHP 代码简洁之道

    https://learnku.com/docs/clean-code-php

    Laravel 编码技巧

    https://learnku.com/docs/laravel-tips/8.x

    laravel 最受欢迎的插件

    https://www.zhihu.com/question/24136962/answer/115970743

    技巧

    需要在日志中查看

    如何打印sql语句

    1. 对象查询eloquent -> toSql()
    $sql_orders = Order::where('user_id', $user_id);
    Log::debug($sql_orders->toSql());
    
    • 1
    • 2

    结果

    [2022-09-01 03:34:32] local.ERROR: select * from `user`  
    
    • 1
    1. DB查询 -> getQueryLog
    DB::enableQueryLog();   // 注意,在查询前,需要启用查询日志
    $sql_orders = Order::where('user_id', $user_id);
    $sql_orders->get();
    Log::debug(DB::getQueryLog());
    
    • 1
    • 2
    • 3
    • 4

    结果

    [2022-09-01 03:19:27] local.DEBUG: array (
      0 => 
      array (
        'query' => 'select * from user',
        'bindings' => 
        array (
        ),
        'time' => 39.77,
      ),
    )  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 监听查询事件

    app/Providers/AppServiceProvider.php boot 方法中添加监听。

    在这里插入图片描述

      public function boot()
        {
            //
            DB::listen(function($query) {
                File::append(
                    storage_path('/logs/query.log'),
                    $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL
                );
            });
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. Laravel Debugbar

    如何把控制器信息输入到控制台

            $user = User::where(['id'=>10]);
            $toSql = $user->toSql();
    
            //Log::error($user);
            $output = new \Symfony\Component\Console\Output\ConsoleOutput();
            $output->writeln("$toSql");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    append的用法

    常用在数据库没这个字段,前端需要返回该字段

    class User extends Model
    {
        use HasApiTokens, HasFactory, Notifiable;
    
        protected $table = "user";
    
        protected $appends = ["isBool","show"];
    
     
        public function getisBoolAttribute(){
            return true;
        }
    
        public function getShowAttribute(){
            return 1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    结果:
    在这里插入图片描述

  • 相关阅读:
    SSL error when connecting to the Jack server. Try ‘jack-diagnose‘
    【Mongoose笔记】HTTP 服务器
    如何实现FPGA的可重复性设计
    面试官:介绍一下 Redis 三种集群模式
    深度学习使用GPU问题
    糟了,生产环境数据竟然不一致,人麻了!
    为什么不建议你用 MongoDB 这类产品替代时序数据库?
    最佳实践 | 顶尖咨询公司常用的解决问题工具
    【Mysql】复合查询
    【JavaEE】Servlet实战案例:表白墙网页实现
  • 原文地址:https://blog.csdn.net/xxpxxpoo8/article/details/126636653