如何动态设置Laravel Model表/集合?

I am using GitHub - jenssegers/laravel-mongodb: A MongoDB based Eloquent model and Query builder for Laravel; In my Laravel project I created DB model that sets Model table name dynamically (in Mongodb case collection) .

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class DbData extends Model
    {   
        protected   $collection = 'default_collection';
        function __construct($collection)
            {
                $this->collection = $collection;
            }
    }

This works when I am creating new DbData object, for data insert:

$data = new DbData('dynamic_collection_name');
$data->variable = 'Test';
$data->save();

But this solution is not enough of I want to use this DbData model for querying data from my database. What I want to achieve is to add possibility to pass variable for DbModel, for instance something like this:

$data = DbData::setCollection('dynamic_collection_name');
$data->get();

You could perhaps do something like this on your class.

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class DbData extends Model
{   
    protected   $collection = 'default_collection';

    public function __construct($collection)
    {
        $this->collection = $collection;
    }

    public static function setCollection($collection)
    {
        return new self($collection);
    }
}

This will allow you to call DbData::setCollection('collection_name') and the collection name will only be set for that specific instance.