Laravel SQL Server更改架构的表名 [英] Laravel sql server change table name for schema
问题描述
我正在一个项目上,我试图在我的SQL Server数据库上设置一些带有模式的表,所以我手动将表名称更改为迁移文件上的schema_name.table_name,这样它可以正常工作这是一个例子:
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');
});
但是,当我尝试在php php artisan tinker
上进行雄辩的查询时,它不起作用,我只是收到一条错误信息,例如进行查询App\CentroVotacion::all();
:
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,消息为'SQLSTATE [42S02]:[Microsoft] [SQL Server的ODBC驱动程序13] [SQL Server]无效的对象名'centro_votacions'. (SQL:从[centro_votacions]中选择*)'
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])'
我当然知道没有表"centro_votacions",有一个名为"electoral.centro_votacions"的表,所以,问题是如何使laravel更改查询,以便可以找到"electoral.centro_votacions"表和我的SQL Server DB上的其他架构?.
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?.
推荐答案
内部CentroVotacion
模型定义表名
class CentroVotacion extends Model
{
protected $table = 'electoral.centro_votacions';
这篇关于Laravel SQL Server更改架构的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!