i am building a project with Laravel 5.1 on a Linux Server. There are two databases one MySQL (no problem here) and MSSQL (problem here). The point is, after a very long research, i finally get the data using mssql funtions like (mssql_query, mssql_fetch_object, etc). My question is related to how can i build an adapter using design patterns inside Laravel architecture, and finally building a driver. Any idea?
Thanks.
Laravel does support MS-SQL out-of-the-box. There's no need to write an adapter. Just configure the 'sqlsrv' connection in your config/database.php and then you have some options to use it:
1 - Directly via DB Facade:
$collection = DB::connection('sqlsrv')->select(...);
$collection = DB::connection('mysql')->select(...);
2 - Setting Model's connection property:
class Foo extends Model {
protected $connection = 'sqlsrv';
...
}
3 - Changing Model's connection at runtime:
$foo = new Foo;
$foo->setConnection('mysql');
$foo->bar = 'baz';
$foo->save();
Please refer to Laravel's Database Docs for more information.
However, if you still need to write an adapter for some reason, the correct way of doing it is writing a Service Provider. Take a look at the implementation of MongoDB Connector as a good reference. Also, I highly recommend you reading the entire "Architecture Foundations" section in Laravel's docs, they are really helpful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With