1
I am using Laravel 5.4 with 2 connections, one of them is just to access some Views of a Sql Server database, and one of them makes a LINKED SERVER which forces me to set some settings before making a query.
I can do it this way today in my Controller:
$busca = DB::connection('meuhost');
$busca->statement('SET ANSI_WARNINGS ON');
$busca->statement('SET ANSI_PADDING ON');
$busca->statement('SET ANSI_NULLS ON');
$select = $busca->select('SELECT top 100 * FROM nome_da_view ');
So it works perfectly, but I didn’t like working that way, so I created a Model to access this View which is as follows:
namespace App;
use Illuminate\Database\Eloquent\Model;
class NomeDaModel extends Model
{
//
protected $connection = 'meuhost';
protected $table = 'nome_da_view';
}
And in Controller:
$busca = NomeDaModel::get();
I tried to set using setAttribute
, statement, exec, nothing worked.
Error occurs in PDO:
exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 7405 General SQL Server error: Check messages from the SQL Server [7405] (severity 16) [(null)]
But we already know what needs to be set in the connection to resolve this error, I just need to know the correct way to do it in Model or Controller other than the way it worked right above.
To solve the problem without the need to set the settings to each query, you can put this in the Model: protected $statement = ['SET ANSI_WARNINGS ON', 'SET ANSI_PADDING ON', 'SET ANSI_NULLS ON'];
– Guilherme Nunes Gonçalves