【问题标题】:Laravel Eloquent model with sqlsrv is changing the table names带有 sqlsrv 的 Laravel Eloquent 模型正在更改表名
【发布时间】:2018-01-24 06:59:01
【问题描述】:

我是 laravel 新手,对使用 php artisan make:model [Name] 创建的模型有疑问

在我的情况下,我连接到 sqlsrv。连接已建立。

我的路线如下所示:

Route::get('/tasks', function () {
    // $tasks = DB::table('WebShops')->get(); //This is working
    $tasks = App\WebShops::all(); //This is not working
    dd($tasks);
});

我的数据表: 网上商店

为此,我创建了一个模型来连接到数据库。 php artisan make:model Webshops.

当我使用命令$tasks = App\WebShops::all(); 访问数据库时出现错误:

SQLSTATE[42S02]:[Microsoft][ODBC Driver 11 for SQL Server][SQL 服务器]无效的对象名称“web_shops”。 (SQL: 选择 * 来自 [web_shops])。

问题是ma表名不同。该查询正在搜索表是 web_shops 的位置,而我的表名是 Webshop。没看懂。

【问题讨论】:

  • 好的解决方案是在模型中添加表格。 protected $table = ....

标签: php sql-server laravel


【解决方案1】:

db 中的名称不写。您不能像 als db name 允许复数那样在彼此后面写 2 个单词。

为了避免你在制作模型时出现这样的问题,你可以这样写

php artisan make:model Wenshop -c -m -r

当你这样写时,Laravel 会为你创建具有正确名称的 Model+ Migration+ Controller

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多