Pick up zip in another bank in Laravel 5

Asked

Viewed 388 times

1

I’m doing a registration system using the Laravel 5, I created the client scheme, with some tables such as person, address, email, phone. happens I have another bank, called cep, where has some tables like street, us, neighborhood, city. I am very beginner in this framework, all I could do was imitate other ready-made models. The Register is working, both the search in the bank, and the inclusion. My question, how do I type the zip in an html and through this zip the input in the form fill in the fields of street, city, neighborhood, state? Thank you.

Note. I made this select in mysql, it worked, now I do not know how to implement in mysql

USE cep;
        SELECT e.endereco_logradouro, b.bairro_descricao, c.cidade_descricao, u.uf_sigla, u.uf_descricao
        FROM endereco e, bairro b, cidade c, uf u
        WHERE e.bairro_codigo = b.bairro_codigo AND
        b.cidade_codigo = c.cidade_codigo AND
        c.uf_codigo = u.uf_codigo AND e.endereco_cep = 'variável'
  • You better configure the database in your application and use it in a specific model of Laravel. He already has his own abstraction, and it will save labor.

1 answer

1

You can configure more than one database on Laravel.

Example:

    'mysql' => array(
      'driver'    => 'mysql',
        'host'      => 'host',
        'database'  => 'database',
        'username'  => 'srvweb',
        'password'  => '...',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => ''
    ),

   'mysql_2' => array(
      'driver'    => 'mysql',
        'host'      => 'host',
        'database'  => 'database_2',
        'username'  => 'srvweb',
        'password'  => '...',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => ''
    ),

Note that you always have various settings on Laravel, for more than one type of SGDB. No problem setting more than one bank for the same SGDB.

You should also noticed another parameter called default that you must configure:

  'default' => 'mysql',

This is the default database used in all your Models.

My suggestion is that you set up another database and, in the model for Cep, you will define that it will use the connection to another database.

    class Cep extends \Eloquent
    {
          protected $connection = 'mysql_2';

          // Outras definições
    }

From then on, just make the consultations with this Model:

   $ceps = Cep::where('endereco', 'LIKE', 'valor%')->get();

It’s just an "illustrative settings". You can use your creativity to better organize your project (and your time).

Browser other questions tagged

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