How to use telescope with moloquent / mongodb?

1.2k views Asked by At

Showing Error

{message: "Call to a member function prepare() on null",…} exception: "Symfony\Component\Debug\Exception\FatalThrowableError" file: "/var/www/html/broc/vendor/laravel/framework/src/Illuminate/Database/Connection.php" line: 326 message: "Call to a member function prepare() on null" trace: [,…]

2

There are 2 answers

0
Yuvraj Singh Shekhawat On BEST ANSWER

Firstly you need to debug that you are using correct DB.

FIle:

vendor/laravel/telescope/src/Http/Controllers/EntryController.php
function : index()

dd($storage)  //show dump 

Output should be :

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

After that

You need to extend EntryModel which is in the vendor/laravel/telescope/src/Storage/DatabaseEntriesRepository.php

location of the telescope package and set moloquent connection there.

now should be use Moloquent instead of Eloquent used like that

//use Illuminate\Database\Eloquent\Model;

use Moloquent as Model;

class EntryModel extends Model

For more detail follow below link

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

A year now from the question date, with Laravel 6, the chosen answer will not suffice.

2 steps are needed to solve the issue:

1- as @Yuvraj mentioned change the extended model in Laravel\Telescope\Storage\EntryModel:

//use Illuminate\Database\Eloquent\Model;

use Moloquent as Model;

class EntryModel extends Model

2- in the same class is a function that scopes the query by the oprtions sent in the request:

    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;
    }

the filter function checks if the record has the property should_display_on_index set to true, which is set as default in the migration: $table->boolean('should_display_on_index')->default(true); But since a mongoDB is used, the default paramater is not respected and hence, is not added to the record.

To resolve this you have two options:

a) comment out the call to filter in the previously mentioned function:

    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;
    }

However, if you are going to use the filter later by setting the should_display_on_index to false, you'll need to modify the filter function instead:

 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;
    }