【问题标题】:How to use telescope with moloquent / mongodb?如何将望远镜与 moloquent / mongodb 一起使用?
【发布时间】:2019-05-13 04:45:05
【问题描述】:

显示错误

{message: "Call to a member function prepare() on null",…} 异常: “Symfony\Component\Debug\Exception\FatalThrowableError”文件: “/var/www/html/broc/vendor/laravel/framework/src/Illuminate/Database/Connection.php” 行:326 消息:“在 null 上调用成员函数 prepare()” 追踪:[,…]

【问题讨论】:

    标签: laravel laravel-5 telescope moloquent


    【解决方案1】:

    距问题日期一年后,使用 Laravel 6,选择的答案还不够。

    需要2个步骤来解决这个问题:

    1- 正如@Yuvraj 所说,更改Laravel\Telescope\Storage\EntryModel 中的扩展模型:

    //use Illuminate\Database\Eloquent\Model;
    
    use Moloquent as Model;
    
    class EntryModel extends Model
    

    2- 在同一类中是一个函数,通过请求中发送的选项来限定查询范围:

        public function scopeWithTelescopeOptions($query, $type, EntryQueryOptions $options)
        {
            $this->whereType($query, $type)
                    ->whereBatchId($query, $options)
                    ->whereTag($query, $options)
                    ->whereFamilyHash($query, $options)
                    ->whereBeforeSequence($query, $options)
                    ->filter($query, $options);
    
            return $query;
        }
    

    filter 函数检查记录是否将属性 should_display_on_index 设置为 true,该属性在迁移中设置为默认值: $table->boolean('should_display_on_index')->default(true); 但是由于使用了 mongoDB,因此不尊重默认参数,因此不会将其添加到记录中。

    要解决此问题,您有两种选择:

    a) 注释掉前面提到的函数中对过滤器的调用:

        public function scopeWithTelescopeOptions($query, $type, EntryQueryOptions $options)
        {
            $this->whereType($query, $type)
                    ->whereBatchId($query, $options)
                    ->whereTag($query, $options)
                    ->whereFamilyHash($query, $options)
                    ->whereBeforeSequence($query, $options)
                    /*->filter($query, $options)*/;
    
            return $query;
        }
    

    但是,如果您打算稍后通过将 should_display_on_index 设置为 false 来使用过滤器,则需要修改过滤器函数:

     protected function filter($query, EntryQueryOptions $options)
        {
            if ($options->familyHash || $options->tag || $options->batchId) {
                return $this;
            }
    
            // $query->where('should_display_on_index', true);
            $query->where('should_display_on_index', '!=', false);
    
            return $this;
        }
    

    【讨论】:

      【解决方案2】:

      首先你需要调试你正在使用正确的数据库。

      文件:

      vendor/laravel/telescope/src/Http/Controllers/EntryController.php
      function : index()
      
      dd($storage)  //show dump 
      

      输出应该是:

      DatabaseEntriesRepository {#1671 #connection: "mongodb"
      #monitoredTags: null }

      之后

      您需要扩展EntryModel,它位于 vendor/laravel/telescope/src/Storage/DatabaseEntriesRepository.php

      望远镜包的位置并在那里设置 moloquent 连接。

      现在应该使用 Moloquent 而不是 Eloquent 那样使用

      //use Illuminate\Database\Eloquent\Model;
      
      use Moloquent as Model;
      
      class EntryModel extends Model
      

      更多详情请点击以下链接

      https://thewebtier.com/php/complete-guide-for-implementing-laravel-telescope-with-mongodb/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 2014-10-21
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多