lundi 26 novembre 2018

Call to undefined method "Connect"

I'm working in a multi-tenant system, I previously tried the https://github.com/hyn/multi- tenant package but in the end I decided to do this implementation with my own method. After a lot of research and testing different things I have managed to build this code, but when I execute it tells me that the connect method is not defined, here I leave what I have done, most likely this is due to some nonsense that I has overlooked, but if someone can help me solve this I would greatly appreciate it

helpers:

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

if(! function_exists('conexionBD')){
 /**
     * Establish a tenant database connection.
     *
     * @param $hostname
     * @param $username
     * @param $password
     * @param $database
     */

    function conexionBD($hostname, $username, $password, $database){
        DB::purge('empresa');

        Config::set('database.connections.empresa.host', $hostname);
        Config::set('database.connections.empresa.database', $database);
        Config::set('database.connections.empresa.username', $username);
        Config::set('database.connections.empresa.password', $password);

        DB::reconnect('empresa');

        Schema::connection('empresa')->getConnection()->reconnect();
    }
}

Empresa Model:

class empresa extends Model
{
    protected $fillable = [
        'hostname',
        'username',
        'password',
        'database'
    ];

    public function ciudad() {
        return $this->belongsTo('confia\ciudad');
    }

    public function empresaUsuario() {
        return $this->hasMany('confia\empresasUsuario');
    }

    public function connect()
    {
        if (! $this->connected()) {
            conexionBD(
                $this->hostname,
                $this->username,
                $this->password,
                $this->database
            );
        }
    }
    /**
     * Check if the current tenant connection settings matches the company's database settings.
     *
     * @return bool
     */
    private function connected()
    {
        $connection = Config::get('database.connections.empresa');
        return $connection['username'] == $this->username &&
            $connection['password'] == $this->password &&
            $connection['database'] == $this->database;
    }
}

Middleware Tenant:

use Closure;
use confia\empresa;

class Tenant{

    protected $company;

    /**
     * Tenant Conctructor
     * @param empresa $empresa
     */

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

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */

    public function handle($request, Closure $next) {
        if (($request->session()->get('empresaId')) === null)
            return redirect()->route('inicio')->withErrors(['error' => __('Por favor inicie sesión en alguna empresa antes de intentar esta acción')]);

        // Get the company object with the id stored in session
        $empresa= $this->empresa->find($request->session()->get('empresaId'));

        // Connect and place the $company object in the view
        $this->connect($empresa);

        return $next($request);
    }
}

In my session variable empresaId I have stored the ID of my company concat to a static string, this is what gives me the name of the database associated with that company.

When I run the system, at the time of making the change of tenant to start working with another database, I get the following error,

"Call to undefined method app\Http\Middleware\Tenant::connect()"

Someone knows that my code is missing or that I am doing wrong to make the multi-tenant system work correctly?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire