Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build custom Laravel database adapter [closed]

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.

like image 216
juancarloscruzd Avatar asked Nov 21 '25 21:11

juancarloscruzd


1 Answers

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!

like image 194
Rafael Beckel Avatar answered Nov 23 '25 12:11

Rafael Beckel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!