Skip to main content

Posts

Showing posts from 2017

Multiple database connections in the same Laravel project

Sometimes there is a situation where we have our main project database, but we need to take some external data from another database – for example, where blog is stored, managed by 3rd party software. How to handle it in Laravel 5? Where to put database credentials There is a file   config/database.php   which contains an array of all possible connections- MySQL, PostgreSQL, SQL Server. So the “trick” here is we can add more than one connection with the same database system, let’s say it will be MySQL. So, let’s copy a default array item and paste as another one underneath – and call it   mysql_external : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33     'connections' => [           'sqlite' => [            ...

Adding foreign keys in phpMyAdmin / Query

phpMyAdmin lets you define foreign keys using their "relations" view. But since, MySQL only supports foreign constraints on "INNO DB" tables, the first step is to make sure the tables you are using are of that type. To setup a foreign key so that the PID column in a table named CHILD references the ID column in a table named PARENT, you can do the following: For both tables, go to the operations tab and change their type to "INNO DB" Make sure ID is the primary key (or at least an indexed column) of the PARENT table. In the CHILD table, define an index for the PID column. While viewing the structure tab of the CHILD table, click the "relation view" link just above the "add fields" section. You will be given a table where each row corresponds to an indexed column in your CLIENT table. The first dropdown in each row lets you choose which TABLE->COLUMN the indexed column references. In the row for PID, choose PARENT->ID fro...