【发布时间】:2021-12-20 09:10:37
【问题描述】:
我的代码是用 PHP 编写的,使用 Laravel 和 Postgresql 作为数据库。
我有一个模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'last_name',
'first_name',
'phone_number',
'address',
];
//protected $table = 'database.contact';
protected $hidden = [
'remember_token',
];
}
和迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Contact extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact ',function(Blueprint $table){
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->integer('phone_number');
$table->ipAddress('address')->nullable();
$table->rememberToken();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact');
}
}
但是当我在终端运行时:
php artisan tinker
然后:
Contact::create(['last_name' => 'joe', 'first_name' => 'ajoe.com', 'phone_number' => 88552])
我得到错误:
Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: 关系“contacts”不存在 第1行:插入“联系人”(“last_name”,“first_name”,“phone_nu ... ^ (SQL: insert into "contacts" ("last_name", "first_name", "phone_number", "updated_at", "created_at") 值 (joe, ajoe.com, 88552, 2021-11-06 21:00:43 , 2021-11-06 21:00:43) 返回 "id")'
如果您知道为什么会发生这种情况,我很想知道 谢谢。
【问题讨论】:
-
欢迎来到 SO ... laravel.com/docs/8.x/eloquent#table-names
-
我试过了,但没有发现错误
标签: php laravel postgresql model tinker