Friday, March 16, 2018

Laravel 5.5, 2 diferent database and diferent model - development

Laravel 5.5, 2 diferent database and diferent model

I have 2 diferent database, I can set 2 different database with 2 diferent 'conection' => ['database01' => [ ... and 'conection' => ['database02' => [ ... in configdatabase.php and in the model with
protected $connection = '1database';
and
protected $connection = '2database';

But I want to use one controller and insert a conditional something like that

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;

if (Session::get('db')=='database01'){
    use AppModel01;
}else{
    use AppModel02;
}

This solution does not work but should be the perfect one.
I can do something like that but now is a lot of time.

public function index()
    {
    if ($baseDat1){
         $data= Data01::orderBy('id', 'desc')->take(25)->get();
    }else{
        $data= Data02::orderBy('id', 'desc')->take(25)->get();
    }        
}

If is possible to do something like I want?



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-5-5-2-diferent-database-and-diferent-model/
via Lzo Media

No comments:

Post a Comment