问题
I'm working on a project and i'm trying to set some tables with a schema on my SQL Server Data Base, so i manually change the tables name to the schema_name.table_name on the migration file and it worked, so here is an example:
Schema::create('electoral.centro_votacions', function (Blueprint $table) {
$table->bigInteger('id_estado');
$table->bigInteger('id_municipio');
$table->bigInteger('id_parroquia');
$table->bigInteger('id');
$table->string('nombre');
$table->longText('direccion');
$table->foreign(['id_parroquia','id_municipio','id_estado'])->references(['id','id_municipio','id_estado'])->on('electoral.parroquias');
$table->primary('id');
});
But when i'm trying to make a eloquent query on php php artisan tinker
it doesn't work i just get an error saying, for example making a query App\CentroVotacion::all();
:
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'centro_votacions'. (SQL: select * from [centro_votacions])'
Of course I get it there is no table "centro_votacions" there is a table called "electoral.centro_votacions", so the thing, is how can i make laravel change the query so that i can find the "electoral.centro_votacions" table and other schemas on my SQL Server DB?.
回答1:
Inside CentroVotacion
model define table name
class CentroVotacion extends Model
{
protected $table = 'electoral.centro_votacions';
来源:https://stackoverflow.com/questions/45003152/laravel-sql-server-change-table-name-for-schema