How to Work with more than one Laravel Table

Asked

Viewed 91 times

0

Good morning.

I am trying to use 2 mysql tables, as if they were 2 schemas, however I am not getting, I tried the examples:

https://www.bugginhoacademy.com.br/blog/trabalhando-com-mais-de-um-banco-de-dados-no-laravel/

Multiple schemas in Laravel 5 database

But it didn’t work, you can help me?

Filing cabinet .env

DB_CONNECTION=banco1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=banco1
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION2=banco2
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=banco2
DB_USERNAME=root
DB_PASSWORD=

Model

<?php

namespace App\Conexoes;

use Illuminate\Database\Eloquent\Model;

class Conexoes extends Model
{
    //
    protected $connection = 'gps';
}

Controller

namespace App\Http\Controllers\Rastreamento;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Conexoes\Positions;
use App\Rastreamento\Markers;
use Mapper;

class PosiceosController extends Controller
{
  private $positions;

  public function index(){
    $this->setConnection('banco2');

   $registros= $this->tabela->all();

}
  • Enter the code you are using and the error that is showing

  • .env = DB_CONNECTION=banco1&#xA;DB_HOST=127.0.0.1&#xA;DB_PORT=3306&#xA;DB_DATABASE=banco1&#xA;DB_USERNAME=root&#xA;DB_PASSWORD=&#xA;&#xA;DB_CONNECTION2=banco2&#xA;DB_HOST=127.0.0.1&#xA;DB_PORT=3306&#xA;DB_DATABASE=banco2&#xA;DB_USERNAME=root&#xA;DB_PASSWORD=&#xA;&#xA;Model:

  • What mistake appears to you?

  • It only finds the first connection and does not find the table because it is on the second connection

  • you said you followed the example of the first link, but it is missing code in your example. Is your code exactly the same as link 1? Another thing the example version was in Laravel 5.4. It’s the same as yours?

  • Yes and the same 5.4

Show 1 more comment
No answers

Browser other questions tagged

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