Dynamically Set Connection in Model Laravel 5.1

Asked

Viewed 169 times

1

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

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

But how can I define it dynamically? Like this: (Which doesn’t work)

class Aparelho extends Model
{
    protected $connection = Auth::user()->conexao;
    protected $table = 'aparelhos';
}

1 answer

1


Well, I found that answer in the English OS that helped me to solve this in the following way:

class Aparelho extends Model
{
    protected $connection;

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

    protected $table = 'aparelhos';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.