• 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

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

  • 相关阅读:
    基于JAVA时间管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    摄像头V4L2获取的YUY2格式转YUV420格式
    Codeforces Round #813 (Div. 2) E. LCM Sum (lcm 离线+树状数组)
    机器学习之算法优化(一)
    jenkins 安装与使用、用户权限划分
    pytorch激活函数
    Java毕业设计之基于SSM实现的图书借阅管理系统
    探索list与iterator的区别及yield的用法
    Linux内存管理(三十三):直接内存回收详解
    c++学习26qt(二)
  • 原文地址:https://blog.csdn.net/xxpxxpoo8/article/details/126636653