![](/img/trans.png)
[英]Laravel/Eloquent - Order By a Model's Relationship Without Using Table Names
[英]Laravel Eloquent model with sqlsrv is changing the table names
我是laravel的新手,并且使用php artisan make:model [Name]
创建的模型有问题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] [用于SQL Server的ODBC驱动程序11] [SQL Server]无效的对象名称“ web_shops”。 (SQL:从[web_shops]中选择*)。
问题是ma表名称不同。 该查询正在搜索表为web_shops且表名称为Webshop的位置。 我不明白
db中的名字不写。 您不能像als db name允许使用复数一样互相写2个字。
为了避免这种问题,您在制作模型时可以这样写
php artisan make:model Wenshop -c -m -r
当您这样写Laravel为您提供具有正确名称的Model + Migration + Controller时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.