Set Connection Dynamically in Model Laravel 5.1

1

I saw that I can use different connections for my models by defining them as follows:

class Aparelho extends Model
{
    protected $connection = 'minha_conexao';
    protected $table = 'aparelhos';
}

But how can I define this dynamically? Type this here: (That does not work)

class Aparelho extends Model
{
    protected $connection = Auth::user()->conexao;
    protected $table = 'aparelhos';
}
    
asked by anonymous 15.08.2016 / 16:14

1 answer

1

Well, I found this answer in the English OS that helped me solve this as follows:

class Aparelho extends Model
{
    protected $connection;

    function __construct()
    {
        return $this->connection = Auth::user()->conexao;
    }

    protected $table = 'aparelhos';
}
    
15.08.2016 / 16:47